5

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") ?>
Ricardo Altamirano
15.3k21 gold badges76 silver badges108 bronze badges
asked Jul 10, 2012 at 10:14
2
  • 1
    Its nice to see a nicely worded first question, bravo! Commented Jul 10, 2012 at 10:16
  • there doesn't seem to be any trace of the analytics script in this html rendered page. Commented Jul 10, 2012 at 10:21

2 Answers 2

4

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');
DavChana
1,96618 silver badges36 bronze badges
answered Jul 10, 2012 at 10:23
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you! Is there any reason why google analytics suggests using: "include_once" rather than "include"?
include_once is better because there are Constants maybe and include_once will not re-define them giving you Warning's.
Yes yes yes!!! Thank you so much! I don't fully understand why but for some reason the PHP pages that go deeper than the root folder could not find the tracking code. This is the final code that fixed it: <?php include_once($_SERVER['DOCUMENT_ROOT'].'/analyticstracking.php'); ?> Thank you all so much (relieved!)
The reason is that include works on the filesystem not the URI - a relative path on the filesystem isn't really the same as a relative URI; the relative URI might be "/analyticstracking.php" BUT the path for include may be something more like "/servers/www/yoursite.com/htdocs/analyticstracking.php" - and it's the $_SERVER['DOCUMENT_ROOT'] variable that holds the path to the root folder defined in the Apache config.
0

You can solve this in many ways. First, you can change the include_path variable http://il.php.net/manual/en/ini.core.php#ini.include-path

Second, you can have your own global variable initialized in index.php with getcwd()

answered Jul 10, 2012 at 10:22

1 Comment

Thanks for your answer. I appreciate your time! Kindest regards.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.