Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

analytics tracking with php website

I have recently changed my website format to php (rather than html), i.e. I have renamed all of my HTML pages with a .php extension and fixed the links with my .htaccess file.

I need to track my new php pages with Google analytics, so I created a separate php file containing Google's javascript snippet. I placed it at the root of my website and linked each of my php tags to it with this code after the <body> tag:

<?php include_once("analyticstracking.php") ?>

My problem is that this only appears to be working with my index.php page. All my other pages cannot find analyticstracking.php (in Dreamweaver it says "'analyticstracking.php' is not on the local disk. Get")

If I change the link (by adding "/") to:

<?php include_once("/analyticstracking.php") ?>

then all my pages can locate the file but google analytics doesn't appear to track my activity.

I am using "Analytics - Real Time" to test this.

Here is my url www.brp-architects.com. (Currently using

<?php include_once("/analyticstracking.php") ?>

as this code, with the "/", allows all my pages to locate my tracking code php file).

The whole reason I am doing this is so I can use a snippet of PHP code to retrieve my website visitor's IP addresses by getting behind the proxy servers ip:

<?
if (getenv(HTTP_X_FORWARDED_FOR)) {
    $ip_address = getenv(HTTP_X_FORWARDED_FOR);
} else {
    $ip_address = getenv(REMOTE_ADDR);
}

?>

Thanks for your responses!

Here is my tracking snippet from the analyticstracking.php file:

<script type="text/javascript">

var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-5434990-2']);
  _gaq.push(['_trackPageview']);
  setTimeout('_gaq.push([\'_trackEvent\', \'NoBounce\', \'Over 30 seconds\'])',30000);

(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>

The link to my tracking code is directly under the opening body tag:

<?php include_once("/analyticstracking.php") ?>
like image 756
user1514406 Avatar asked Oct 07 '22 06:10

user1514406


1 Answers

If it works on index.php, then the analyticstracking.php script is fine.

What could cause this is the way you include this on other Scripts.

Just make sure the path of that script is correct on other Scripts.
To be sure include like this :

include($_SERVER['DOCUMENT_ROOT'].'PATH-TO-SCRIPT/analyticstracking.php');
like image 115
Rosmarine Popcorn Avatar answered Oct 11 '22 01:10

Rosmarine Popcorn