'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 1:[1]

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.

Solution 2:[2]

I've found that the new universal analytics is actually a little slow to kick in within the interface. The old Google analytics used to be like this and could take a day to work.

I have set up universal analytics on 6 sites now, I could see the calls being made to google but it took a day for data to show.

Have you used any debugging tools available.

For Firefox httpfox is a great tool, once installed select start and filter by 'collect' this will display analytics calls if they are working.

In chrome there are 2 good extensions, tag assistant and ga debug both by Google. Both of these can help identify implementation issues.

Hope this helps

John

Solution 3:[3]

Credit: https://towardsdatascience.com/advancing-to-professional-dashboard-with-python-using-dash-and-plotly-1e8e5aa4c668#cf77 Try this

import dash

app = dash.Dash(__name__)

app.index_string = """<!DOCTYPE html>
<html>
    <head>
        <!-- Global site tag (gtag.js) - Google Analytics -->
        <script async src="https://www.googletagmanager.com/gtag/js?id=UA-131327483-1"></script>
        <script>
          window.dataLayer = window.dataLayer || [];
          function gtag(){dataLayer.push(arguments);}
          gtag('js', new Date());

          gtag('config', 'UA-131327483-1');
        </script>
        {%metas%}
        <title>{%title%}</title>
        {%favicon%}
        {%css%}
    </head>
    <body>
        {%app_entry%}
        <footer>
            {%config%}
            {%scripts%}
            {%renderer%}
        </footer>
    </body>
</html>"""

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nitzan Shaked
Solution 2 confidentjohn
Solution 3