Search code examples
herokugoogle-analyticssubdomain

Google analytics doesn't work on my heroku app


I run a python web app on heroku platform,

using heroku sub domain like: example.herokuapp.com

and I add google analytics js code to my page:

  (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', 'UA-xxxxxxxx-x', 'herokuapp.com');
  ga('send', 'pageview');

I have insert this script over 12 hours, but when I enter my ga account admin panel, it tell me:

The Google Analytics tracking code has not been detected on your website's home page. For Analytics to function, you or your web administrator must add the code to each page of your website.

however, I still can see some PV on the report panel

Does the ga work or not? How can I solve this problem? If I use a top level domain could solve this problem?


Solution

  • Two things regarding your ga.create() call:

    1) The third parameter ("herokuapp.com" in your case) is supposed to be a Javascript object, not a plain string. Did you actually mean {'cookieDomain': 'herokuapp.com'}?

    2) If you did... then it should be example.herokuapp.com -- you can't set a cookie on all of herokuapp.com.