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

LVL 11
Andrew AngellCo-Owner / DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tony McCreathTechnical SEO ConsultantCommented:
From what I understand, you are sending an email with the analytics code in it.

I think most email readers will not execute javascript, so your code will not get run. It may also cause your emails to be flagged as suspicious or spam.

You want to have the javascript apear on the thankyou page that I presume you generate after sending the email.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Andrew AngellCo-Owner / DeveloperAuthor Commented:
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.
0
Tony McCreathTechnical SEO ConsultantCommented:
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?
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Andrew AngellCo-Owner / DeveloperAuthor Commented:
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.
0
Tony McCreathTechnical SEO ConsultantCommented:
I see the issue then. Unfortunately, I can't think of a solution.
0
Andrew AngellCo-Owner / DeveloperAuthor Commented:
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

0
Andrew AngellCo-Owner / DeveloperAuthor Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.