Solved

Javascript, Google Analytics, and PHP

Posted on 2008-10-09
7
561 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.

746 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now