3rdLifeWebDev
asked on
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.j s' type='text/javascript'%3E% 3C/script% 3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-xxxxx x-x");
pageTracker._setDomainName ("none");
pageTracker._setAllowLinke r(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._lin kByPost(th is)">
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.
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
document.write(unescape("%
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-xxxxx
pageTracker._setDomainName
pageTracker._setAllowLinke
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
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._lin
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);
?>
ASKER
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.
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.
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 ?
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 ?
ASKER
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=1 00
SiteAview_product.htm?id=1 20
SiteAadd_to_cart.htm?id=12 0 _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
What we have is more like:
SiteA.Index.htm
SiteAview_product.htm?id=1
SiteAview_product.htm?id=1
SiteAadd_to_cart.htm?id=12
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
ASKER
This may be simpler than I thought.
Please review:
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.--/>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I have completed the code. I am giving 24 hours for Google to track.
I will close this tomorrow. Thanks for your help.
I will close this tomorrow. Thanks for your help.
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.