Welcome To Jon Waraas dot Com
Track Adwords Conversion With Php
January 1st, 2009 by Jon Waraas


Happpy New Years everyone! This will be my last blog post of 2008!

I just made this tiny script to help me track my adwords conversions accurately. Up until this point I have been using a few html pages with the Adwords Conversion html code on it. But since I have over 15 different products per site my Cost per Conversion was off. So this script accurately tracks my cost per conversion.

The code is pretty simple. All you will be doing is sending the amount the product sold for through the paypal return feature. And then having a piece of code that will grab the amount and add it into the Adwords Conversion html code.

I’m using paypal for all of my e-commerce websites, its simple, reliable, and safe. Below would be a normal “Buy Now” button html code. But instead of using a basic /sold.php page I am using /sold.php?id=9.99. The 9.99 would be the cost of the product you are selling ($9.99 dollars).

(I took out the non-important fields on the paypal html below)

<form action=”https://www.paypal.com/cgi-bin/webscr” method=”post”>
<input type=”hidden” name=”business” value=”email@domain.com”>
<input type=”hidden” name=”item_name” value=”Product Name”>
<input type=”hidden” name=”return” value=”http://www.domain.com/sold.php?id=9.99″>
<input type=”hidden” name=”cancel_return” value=”http://www.domain.com/cancel.php”>
<input type=”hidden” name=”currency_code” value=”USD”>
<input type=”hidden” name=”amount” value=”9.99″>
</form>

So now when the customer purchases the product from your website, they will then pay using paypal, and then once they get done paying then paypal will redirect them to your return page. Which would be http://www.domain.com/sold.php?id=9.99.

On the sold.php you will want to let your customer that you are happy that they ordered from you. You will also want to tell them important info about the product and/or shipping. And finally you will want to try and sell them some other stuff. So list some other product and give them a special discount for being a return customer. Maybe they just might order more from you.

Also on the sold.php you will want to put the code that will grab the 9.99 out of the url and add it into the Adwords Conversion html code. The code is very simple, basically all you need to do is find out if there is a variable in the url and if so grab it and do something with it.

So we will use isset to see if there is a variable. If so we will grab it using strip_tags(), then we will echo it onto the page. Very simple, take a look at the code below.

(But this in your body tags, make sure to edit and add your own Adwords Conversions tracking html code)

<?php

if (!isset($_GET['id'])) {
// you dont need anything here
} else {
$id = mysql_escape_string(strip_tags($_GET['id']));

?>
<script language=”JavaScript” type=”text/javascript”
<!–
var google_conversion_id = 1010101;
var google_conversion_language = “en_US”;
var google_conversion_format = “1″;
var google_conversion_color = “ffffff”;
var google_conversion_label = “1010101″;
if (<?php echo “$id”; ?>) {
var google_conversion_value = <?php echo “$id”; ?>;
}
//–>
</script>
<script language=”JavaScript” src=”http://www.googleadservices.com/pagead/conversion.js”>
</script>
<noscript>
<img height=”1″ width=”1″ border=”0″ src=”http://www.googleadservices.com/pagead/conversion/1010101/?value=<?php echo “$id”; ?>&label=7LUOCO_xdRDblMj9Aw&script=0″/>
</noscript>
<?php } ?>

Ok so let me explain the code a little. I used isset see if there is a variable in the url, if their is it will grab it, if their isn’t it won’t do anything. Now there are a few different ways to use isset, you can check them out here. In this case the variable would be 9.99 because the url is http://www.domain.com/sold.php?id=9.99. I then used strip_tags() to get the variable out of the url. And I also used mysql_escape_string() to be secure. Then all I do is echo the variable in the Google Adwords Conversion Tracking html code.

You can use this piece of code to make it easier for you to track conversions. Instead of making 15 different sold.php pages you just need to make one. Let me know if you have any questions.

Follow me on my journey of success and failures, it should be a fun ride.... Again, here is the link for the RSS feed.

Random Posts

Comments are Dofollow! My thanks to you for contributing to the discussion

7 Comments

Comment by Chris M
2009-01-01 16:01:48

Cool stuff, I actually did something very similar not too long ago, however, coming from South Africa, we’re not allowed to use PayPal :/

 
Comment by Pandora Battery
2009-01-03 17:14:38

Jon! Your slakin on updates bro. lol.

 
Comment by anon
2009-01-05 04:29:19

There’s actually a better way to do this in my opinon. With this you have to edit your return URL for each product (suppose its not a big deal.

But the way I’ve done it is to set my return url in my paypal account, there’s a setting under your profile somewhere.

I set it to always go to thanks.php after a transaction is completed. And I turned on payment data transfer.

Then you just stick this code at the top of your page:

https://www.paypal.com/us/cgi-bin/webscr?cmd=p/pdn/pdt-codesamples-pop-outside#php

And paypal will send the page some of the transaction details, including $amount which contains how much was charged. Make sure you replace the PDT token in there with the one given to you by paypal after switching on PDT.

This is more efficient because you don’t have to worry about setting a proper return URL every time you add or update a product. It also forces the user to be redirected back to your thankyou page, I’m not 100% certain that just setting a return url in the form code does that.

 
Comment by BlogTalks
2009-01-05 14:30:23

Interesting use of the form post method, I would not have thought of doing that or in fact realised it would have worked.

 
Comment by Chris M
2009-01-10 10:24:34

How is the conversion tracking coming along Jon?

 
Comment by Web Design Beach
2009-01-12 16:00:40

Thanks for sharing. Tracking conversations is a must if you want to succeed with adwords or any other advertising campaign.

 
Comment by Nomar
2009-01-13 15:28:31

Here rates are around 5% yearly

 

Sorry, the comment form is closed at this time.