AdWords is now Google Ads. Our new name reflects the full range of advertising options we offer across Search, Display, YouTube, and more. Learn more

Analytics
4.7K members online now
4.7K members online now
Dive into multiple domain (Cross/Sub) tracking, implementing Ecommerce and Enhanced Ecommerce, setting up Event tracking, and Universal Analytics code.
 
Guide Me
star_border
Reply

Tracking Code PHP Implementation

[ Edited ]
Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

Hi,

 

I am trying to add tracking code with PHP implementation to my website.

 

So I have generated an analyticstracking.php file and put it into root of my website where all other php files are at inside public_html folder, and added the provided line to the end of my header.php in the body part.

 

However, when I view the source code, I do not see the tracking code script, instead I see the one added line in green color.

 

What is the reason that the line does not load the analyticstracking.php file? And therefore GA doesn't work obv.

Thank you in advance.

 

Screenshot link: https://ibb.co/eUpuaQ

 

1 Expert replyverified_user
1 ACCEPTED SOLUTION

Accepted Solutions
Marked as Best Answer.
Solution
Accepted by topic author Halid B
July 2017

Re: Tracking Code PHP Implementation

Top Contributor
# 12
Top Contributor

Back again :-)

Ok..looking at this.. there are two things I would try..

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

 

replace with
{php} include_once("analyticstracking.php"); {/php}

 

or try


<script type="text/javascript">/*<![CDATA[*/
// {literal}
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-XXXX-X', 'auto');
ga('send', 'pageview');
// {/literal}
/*]]>*/</script>

Bronwyn Vourtis, Google Analytics Top Contributor
Was my response helpful? If yes, please mark it as the ‘Best Answer.’ Learn how here

View solution in original post

Tracking Code PHP Implementation

[ Edited ]
Top Contributor
# 2
Top Contributor

Hi @Halid B :-)

Is your site built with WordPress or another CMS?

 

eta..also your image indicates the tracking include is not in the <head></head> section but before the closing </body> tag

 

 

Bronwyn Vourtis, Google Analytics Top Contributor
Was my response helpful? If yes, please mark it as the ‘Best Answer.’ Learn how here

Tracking Code PHP Implementation

Visitor ✭ ✭ ✭
# 3
Visitor ✭ ✭ ✭

Thanks for your reply Bronwyn.

 

Website is built with basic php, no cms.

 

I've tried including it in the head section too, result is the same: include line stays green, I assume it means that it cannot read the line properly.

 

Any other ideas?

Tracking Code PHP Implementation

Top Contributor
# 4
Top Contributor

You dont necessarily need a php include.

have you tried simply pasting the js tracking code snippet to header.php before the closing </head> tag?

Bronwyn Vourtis, Google Analytics Top Contributor
Was my response helpful? If yes, please mark it as the ‘Best Answer.’ Learn how here

Tracking Code PHP Implementation

Visitor ✭ ✭ ✭
# 5
Visitor ✭ ✭ ✭

Yes I did try, both before closing</head> or</body> tag. Website stops working either way, which I found really surprising.

 

This is what I'm pasting, directly from google analytics page. I really think I should be making a very simple mistake somewhere.

 

<?php
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-XXX', 'auto');
ga('send', 'pageview');

</script>
?>

 

I tried without the php tags too, I dont know if that'd make any difference. The result was the same, website stopped working.

 

Thanks again for your help.

Tracking Code PHP Implementation

Top Contributor
# 6
Top Contributor

Are you able to provide a link to the site with the line of code that you were using in your screen shot?

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

 

thanks :-)

Bronwyn Vourtis, Google Analytics Top Contributor
Was my response helpful? If yes, please mark it as the ‘Best Answer.’ Learn how here

Tracking Code PHP Implementation

Visitor ✭ ✭ ✭
# 7
Visitor ✭ ✭ ✭

yes sure,

 

www.alfasanayi.com

 

You can see the lines both before closing</head> or</body> tags in the source code, in green.

 

Thank you for your interest!

Re: Tracking Code PHP Implementation

Top Contributor
# 8
Top Contributor

Thank you :-)

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

from before the closing </body> tag

in analyticstracking.php

copy the following (obviously with your tracking id in the snippet)

 

<?php

echo "<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-XXX'', 'auto');
  ga('send', 'pageview');

</script>";

?>

and depending on what is in header.php, you may not need 

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

 

you might just need instead

include_once("analyticstracking.php");

 

Bronwyn Vourtis, Google Analytics Top Contributor
Was my response helpful? If yes, please mark it as the ‘Best Answer.’ Learn how here

Tracking Code PHP Implementation

Visitor ✭ ✭ ✭
# 9
Visitor ✭ ✭ ✭

So I've added the echo part to analyticstracking.php and tried include line in header file both with and without <?php tags , still no result Smiley Sad

 

Without <?php tag, the line appears at the top of the page (not the source code, I mean the it appears on the normal website as a straight font sentence.

 

With the php tag it still stays green

Tracking Code PHP Implementation

Top Contributor
# 10
Top Contributor

 

Hi :-)
the site is html is that correct?
all the links to other site pages are links to html pages is that correct?

eg: http://www.alfasanayi.com/urunler-hepsi.html

From what I am looking at.. and I'm taking a guess here..

your html doesnt seem to be parsing php correctly.

I can access the analyticstracking.php and view its source code without issues

via http://www.alfasanayi.com/analyticstracking.php

however

http://www.alfasanayi.com/header.php gives a 404 error

 

Without having access/knowledge of your site, its a bit hard to work out what is up since no errors are coming up in the browser or dev tools.

 

I'd be checking file paths and considering an apache/php handler

Bronwyn Vourtis, Google Analytics Top Contributor
Was my response helpful? If yes, please mark it as the ‘Best Answer.’ Learn how here