Javascript, Google Analytics, and PHP

Goal:

Track info passed to 3rd party shopping cart with GA .

Here are the instructions:

(1) Add the following lines to your tracking code on all your  website and on your  shopping cart pages.

This code must occur above the code in Step 2.

<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-x");
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._trackPageview();
</script>

(2) Change the links from the main site to the secure site to use _link as follows. Please note that your analytics tracking code and
calls to _gat._getTracker (shown in Step 1) must be placed on the page above the call to _link. If your current links look like:

<a href="https://www.securecart.com/?store=parameters">
Purchase Now
</a>

change them to:

<a href=" https://www.securecart.com/?store=parameters" onclick="pageTracker._link(this.href); return false;">Purchase Now</a>

The code above provides links for users with or without JavaScript enabled. It's important to note that apostrophes need to be
escaped with a backslash where they appear in the link or link text.

If you send information to your shopping cart using forms, use:

<form name="post_form" method="post" onsubmit="pageTracker._linkByPost(this)">

Important: if your pages include a call to trackPageview(), link(), trackTrans(), or linkByPost(), your Analytics tracking code
p y pg _ g (),_ (), _ (), _ y (), y y g
must be placed in your HTML code above any of these calls. In these cases the tracking code can be placed anywhere between the
opening <body> tag and the JavaScript call.

This would be fine EXCEPT that the Form Action presently passes code to a .PHP page, which inturn passes to the 3rd party shopping cart page.

NEEDED SOLUTION:

Code to add to PHP which would function as the Javascript "onclick" so that GA will receive tracking from this PHP page.



<?php
 
$cartUrl = "https://myshoppingcart.com/cart/?update=true&l=n";
 
foreach($_POST as $k=>$v){
 
	if($v != "" && strchr($k,"p") && $k != "pcode"){
		
		$cartUrl .= "&product_id=" . substr($k,1,strlen($k)) . "&" . $k . "_qty=" . $v;		
		
	}else if($v != "" && strchr($k,"c") && $k != "pcode"){
	
		$cartUrl .= "&cprogram_id=" . substr($k,1,strlen($k)) . "&" . $k . "_qty=" . $v;
		
	}else if($v != "" && $k == "pcode"){
		
		$cartUrl .= "&promocode=" . $v;
		
	}
 
}
 
Header("Location: " . $cartUrl);
 
?>

Open in new window

3rdLifeWebDevAsked:
Who is Participating?
 
edster9999Commented:
That will only work if you have output buffereing turned on -

If you put the javascript code above that will fail too as you can not put any text out before a header command unless you use buffer.  You can enable this in php.ini or by using the
 ob_start();
and
 ob_flush();
commands
0
 
edster9999Commented:
You are adding it in the wrong place.
This code takes details from a form or another page and then creates a new link to a page where the details get updated
(seems like a pretty strange way of doing it...but there you go...)
You would need to go back a step to the page that calls this and add it in there.
0
 
3rdLifeWebDevAuthor Commented:
I would, except that the form action calls the PHP page listed above, NOT the 3rd party shopping cart.

Unless you are saying that the PHP page that passes the info from front site to 3rd party shopping cart will also pass info back to GA. I don't see that it does, without the GA javascript being on that page, or there being PHP script is adjusted to do the same.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
edster9999Commented:
The GA code is to place on the page the user sees.  It runs when they click a link or button.  It should be on the front end page.

You can also do a more basic level of GA where you put script on every page and you then set up shopping paths using the page names

example a path could be something like

Index.htm
view_product.htm?id=100
view_product.htm?id=120
add_to_cart.htm?id=120
checkout.htm

is this what you are after or am I missing the point here ?
0
 
3rdLifeWebDevAuthor Commented:
I thought that the GA code needed to be on every page in order to track correctly. If there is a page in the chain that do not have the code then the info is not passed on that page, thus breaking the chain.

What we have is more like:

SiteA.Index.htm
SiteAview_product.htm?id=100
SiteAview_product.htm?id=120
SiteAadd_to_cart.htm?id=120  _onclick(hidden.php)
hidden.php //contains code to update Site3rdParty.checkout.htm  (This is the page that needs to GA code conversion, or so my logic dictates.)
Site3rdParty.checkout.htm


0
 
3rdLifeWebDevAuthor Commented:
This may be simpler than I thought.

Please review:

<?php
 
$cartUrl = "https://myshoppingcart.com/cart/?update=true&l=n";
 
foreach($_POST as $k=>$v){
 
        if($v != "" && strchr($k,"p") && $k != "pcode"){
                
                $cartUrl .= "&product_id=" . substr($k,1,strlen($k)) . "&" . $k . "_qty=" . $v;         
                
        }else if($v != "" && strchr($k,"c") && $k != "pcode"){
        
                $cartUrl .= "&cprogram_id=" . substr($k,1,strlen($k)) . "&" . $k . "_qty=" . $v;
                
        }else if($v != "" && $k == "pcode"){
                
                $cartUrl .= "&promocode=" . $v;
                
        }
 
}
 
Header("Location: " . $cartUrl);
 
?>
 
<script type="text/javascript">
 
<form onload="pageTracker._linkByPost(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-x");
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._trackPageview();
</script>
 
<-- In essence, I am letting the PHP deal with the designated script it was intended to deal with passing the data forward to the 3rd party shopping cart, and then letting javascript pass the info back to GA.--/>

Open in new window

0
 
3rdLifeWebDevAuthor Commented:
I have completed the code. I am giving 24 hours for Google to track.

I will close this tomorrow. Thanks for your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.