Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Javascript, Google Analytics, and PHP

Posted on 2008-10-09
7
Medium Priority
?
586 Views
Last Modified: 2009-08-12
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

0
Comment
Question by:3rdLifeWebDev
  • 4
  • 3
7 Comments
 
LVL 20

Expert Comment

by:edster9999
ID: 22683208
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
 

Author Comment

by:3rdLifeWebDev
ID: 22683822
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
 
LVL 20

Expert Comment

by:edster9999
ID: 22685111
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:3rdLifeWebDev
ID: 22685562
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
 

Author Comment

by:3rdLifeWebDev
ID: 22692392
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
 
LVL 20

Accepted Solution

by:
edster9999 earned 2000 total points
ID: 22694385
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
 

Author Comment

by:3rdLifeWebDev
ID: 22790028
I have completed the code. I am giving 24 hours for Google to track.

I will close this tomorrow. Thanks for your help.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question