Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialTunde Adegoroye
20,597 Pointsgoogle analytics not working
For some reason google analytics seems to not be installing although it is visible on my page source i have used a php file and included the code and also tested just copying the js code into the head tag here is my site www.tunds.me Here is the code that i inserted into my php file i have removed the ID for obvious reasons
<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','//www.google-analytics.com/analytics.js','ga');
ga('create', 'ID GOES HERE', 'auto');
ga('send', 'pageview');
</script>
4 Answers
Dustin Matlock
33,856 PointsIt didn't look like it was installed correctly before, but It does take 24 hours according to Google. I'd recommend putting it right before the closing </body>
tag for better performance.
For instance:
<script>
(function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]=
function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date;
e=o.createElement(i);r=o.getElementsByTagName(i)[0];
e.src='//www.google-analytics.com/analytics.js';
r.parentNode.insertBefore(e,r)}(window,document,'script','ga'));
ga('create','UA-54813635-1');ga('send','pageview');
</script>
Dustin Matlock
33,856 PointsOddly, I've had the same thing happen to me a few times. The best and most immediate solution for Wordpress is the Yoast plugin, or if you use CloudFlare, you can easily add Google Analytics via the apps.
The code block needs to be in the head
sectionโlooks like you have it in the body. I highly recommend CloudFlare for caching and easy GA integration. Also, check out Google's documentation for installing GA.
Tunde Adegoroye
20,597 PointsI have checked and it is in the head section also reading other forums it does say it can take unto 2 days before the tracking status can change think i'll leave it until then but how long has it normally taken you for google to realise you have the code in your experience
Chris Southam
1,823 PointsThis looks all good to me, I've checked your website and using Google's Tag Assistant extension for Chrome it confirms that there's a tracking code on the page. It can some time for Google to pick up and process the stats - rest assured that if you've got your ID right then the tracking is happening.
Tunde Adegoroye
20,597 PointsThanks for all your help guy i was a little impatient but it seems to be working now :D
Dustin Matlock
33,856 PointsDustin Matlock
33,856 PointsTunde, for some reason I thought you were using Wordpress. My answer below has been updated to reflect what should be changed. Hope it helps.