Link to home
Start Free TrialLog in
Avatar of Andrew Angell
Andrew AngellFlag for United States of America

asked on

Can I run Google Analytics tracking code within a PHP web service script...???

I'm trying to get Google Analytics E-Commerce and AdWords Conversion Tracking configured for my web site and I'm having some issues.  

I'm using PayPal's IPN (instant payment notification) system to process orders and I've got the analytics code dynamically generated within this script.  

I was a little worried at first that because the code is included in a page that never actually loads in a browser that might be the problem.  However, I found the following link that seems to prove otherwise:

http://books.google.com/books?id=eio4QY-wrEkC&pg=PA259&lpg=PA259&dq=paypal+ipn+adwords+conversion&source=bl&ots=htQ1hqeJ-2&sig=14lZc7DIYmrnWQK1NEclN5HamQY&hl=en&ei=PvQOSvTFJ4KMtgfRr7SNCA&sa=X&oi=book_result&ct=result&resnum=3#PPA259,M1

I've attached the PHP code that generates all of my google tracking code here.  I'm afraid I'm a dummy and I deleted the samples I had in my email box so I'll have to wait on some new orders to provide fresh samples of what actually gets generated from this code, however, I can tell you that when comparing it to Google's samples (http://www.google.com/support/googleanalytics/bin/answer.py?hl=en&answer=76305#ecommerce) it's the same.  Nothing shows up in my Analytics E-Commerce or AdWords conversion areas though.

Any information on how I can get this working would be greatly appreciated.  Thanks!!!
<?php
# Google Analytics E-Commerce Tracking Code
if(!$sandbox && $google_analytics_ecommerce && strtolower($payment_status) == 'completed')
{
	$js_output = '<script type="text/javascript">
			var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
			document.write(unescape("%3Cscript src=\'" + gaJsHost + "google-analytics.com/ga.js\' type=\'text/javascript\'%3E%3C/script%3E"));
			</script>
			
			<script type="text/javascript">
			  var pageTracker = _gat._getTracker("' . $google_analytics_id . '");
			  pageTracker._trackPageview();
			  pageTracker._addTrans(
				"' . $invoice . '",           // Order ID
				"",                            // Affiliation
				"' . $mc_gross . '",           // Total
				"' . $tax . '",                // Tax
				"' . $shipping . '",           // Shipping
				"' . $address_city . '",       // City
				"' . $address_state . '",      // State
				"' . $address_country . '"     // Country
			  );';
		
	foreach($cart_items as $cart_item)
	{
		$js_output .= 'pageTracker._addItem(
				"' . $invoice . '",                                     // Order ID
				"' . $cart_item['item_number'] . '",                     // SKU
				"' . $cart_item['item_name'] . '",                       // Product Name 
				"",                            							 // Category
				"' . $cart_item['mc_gross'] . '",                        // Price
				"' . $cart_item['quantity'] . '"                         // Quantity
			  );';		
	}
			
	$js_output .= 'pageTracker._trackTrans();
			</script>';
			
	echo $js_output . chr(10) . chr(10);
}
 
$js_output_email = isset($js_output) ? $js_output : '';
 
# Google Analytics AdWords Conversion Tracking
if(!$sandbox && $google_adwords_conversions && strtolower($payment_status) == 'completed')
{
	$js_output = '<script language="JavaScript" type="text/javascript">
					<!--
					var google_conversion_id = ' . $google_adwords_conversion_id . ';
					var google_conversion_language = "en_US";
					var google_conversion_format = "3";
					var google_conversion_color = "ffffff";
					var google_conversion_label = "XNx1CM7GWBCmxJr2Aw";
					if (' . number_format($mc_gross,1) . ') {
					  var google_conversion_value = ' . number_format($mc_gross,1) . ';
					}
					//-->
					</script>
					<script language="JavaScript" src="https://www.googleadservices.com/pagead/conversion.js">
					</script>
					<noscript>
					<img height="1" width="1" border="0" src="https://www.googleadservices.com/pagead/conversion/' . $google_adwords_conversion_id . '/?value=' . number_format($mc_gross,1) . '&amp;label=XNx1CM7GWBCmxJr2Aw&amp;guid=ON&amp;script=0"/>
					</noscript>';
					
	echo $js_output;	
}
 
# Temp to see if this get reached
if(isset($js_output))
{
	$js_output_email .= $js_output;
	$mail -> Subject  =  'USBSwiper PayPal IPN : Google Analytics Code Ran';
	$mail -> Body =  $js_output_email;
	$mail -> AddAddress($admin_email_address, $admin_name);
	$mail -> Send();
	$mail -> ClearAddresses();	
}
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Tony McCreath
Tony McCreath
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Andrew Angell

ASKER

No, the javascript is being output on the page.  I'm simply emailing myself the same thing that gets output to the page so that I can compare it with what google's samples show.  Since it doesn't actually get run within a browser I have to either do that or save a file.  But if you look at the PHP code you'll see each section of $js_output gets printed to the screen and then the collection of those ($js_output_email) is what I'm emailing myself just to see what it did.

There is a way I can get this same script to load within a browser and when I do I wind up with nothing but that javascript code in the page when I view source as expected.
If its output to the page, how is it not being executed in the browser. I don't quite get it?

Is it that paypal is actually making the request, not the visitor?
Right, PayPal simply POSTs the data to a script sitting on the server.  You can see the page getting hit in your web server logs, you can save files, send emails, whatever you wanna do within it.  Even write text to the page.  It just doesn't happen within a browser so nobody would ever see it.  

As far as the web server is concerned, though, the page loaded and the code executed successfully.
I see the issue then. Unfortunately, I can't think of a solution.
I just got a new order and here's a sample of the javascript that was generated within this script...
<script type="text/javascript">
			var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
			document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
			</script>
<script type="text/javascript">
			  var pageTracker = _gat._getTracker("UA-XXXXXX-1");
			  pageTracker._trackPageview();
			  pageTracker._addTrans(
				"752",           // Order ID
				"",                            // Affiliation
				"107.39",           // Total
				"0.00",                // Tax
				"0",           // Shipping
				"Broken Arrow",       // City
				"OK",      // State
				"United States"     // Country
			  );pageTracker._addItem(
				"752",                                     // Order ID
				"2",                     // SKU
				"USBSwiper Software License(s)",                       // Product Name 
				"",                            							 // Category
				"0.00",                        // Price
				"2"                         // Quantity
			  );pageTracker._addItem(
				"752",                                     // Order ID
				"1",                     // SKU
				"USBSwiper Magnetic Stripe Credit Card Reader",                       // Product Name 
				"",                            							 // Category
				"99.94",                        // Price
				"2"                         // Quantity
			  );pageTracker._trackTrans();
			</script>
<script language="JavaScript" type="text/javascript">
					<!--
					var google_conversion_id = 1053205030;
					var google_conversion_language = "en_US";
					var google_conversion_format = "3";
					var google_conversion_color = "ffffff";
					var google_conversion_label = "XNx1CM7GWBCmxJr2Aw";
					if (107.4) {
					  var google_conversion_value = 107.4;
					}
					//-->
					</script>
<script language="JavaScript" src="https://www.googleadservices.com/pagead/conversion.js">
					</script>

Open in new window

I've gone beyond this and I've actually saved the cookie data for each order to my database and then within the IPN script I pull it back out and I generate the same <gif calls to google that the javascript generates.  

Still no joy, though.  :(  I thought for sure I had it that time.