Solved

Javascript, Google Analytics, and PHP

Posted on 2008-10-09
7
570 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

 

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 500 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

636 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