Development Blog

February 28, 2013
Track same page name with Google Analytics.
GA



Our client wants to run Google Analytics on their online image library and the standard tracking code will not report correctly because it returns the same page name, ie results.php and details.php. This is also true for most online stores where the pages are dynamicly driven to build a single page.

GA recommends the dynamic content implementation and here's what we have done with our master template file. IF results.php do this, ELSE IF details.php do this, and ELSE use the standard tracking code. Inside the _trackPageview value, we defined with our PHP codes so it is unique to that page, ie key word search or image number.

RAW

<?php

$pagepath = $_SERVER["PHP_SELF"];
$page = substr($pagepath, strrpos($pagepath, '/') + 1);
		
if ($page == "results.php"){

?>

<!-- GA search tracking, results.php -->
<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-12345678-9");
pageTracker._initData(); 
pageTracker._trackPageview('/library/results.php?Search=<?php
    if(isset($_POST["SubmitType"]))
    {
        switch($_POST["SubmitType"])
        {
            case "SearchText":
                if (isset($_POST["Search"]))
                {
                    if (isset($_POST["Search"]))
                    {
                        echo $_POST["Search"];
                    }
                }
                break;
            case "Search":
                if (isset($_POST["keyword"]))
                {
                    if (isset($_POST["keyword"]))
                    {
                        echo Join(" ",$_POST["keyword"]);
                    }
                }
                break;
        }
    }
    else
    {
        echo Join(" ",$InvSession->GetKeywords());
    }
    
    ?>'); 
</script>

<?php } else if ($page == "details.php"){

?>

<!-- GA detail page tracking, details.php -->
<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-12345678-9");
pageTracker._initData(); 
pageTracker._trackPageview('/library/details.php?ItemID=<?php 
	$ID = $InvSession->GetItemID();
	echo $ID; 
	// echo $ItemCode;
?>'); 
</script>

<?php } else { ?>

<!-- GA standard tracking -->
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-12345678-9']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 
    	'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 
    	'https://ssl' : 'http://www') + 
    	'.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; 
    s.parentNode.insertBefore(ga, s);
  })();

</script>

<?php } ?>




RESULT
Page Pageviews
1. / 516
2. /welcome.php 190
3. /library/results.php?Search=strategy 89
4. /library/details.php?ItemID=39 12
Home | Portfolio | Expertise | Clients | People | Contact | Privacy Policy | Copyright Policy
Copyright © 2024 Pinpoint Design LLC. All Rights Reserved.