Forums

Site crashing after adding Google Analytics code. How do I add GA?

Hi I'm adding my GA script code and its crashing. I'm using Flask framework V3.6. and This is the code I've added in my flask_app.py file . Please advice how can I add GA without crashing the site.

return '''

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

    </script>
    </head>

    <body>
        {errors}

[edited by admin: code formatting]

A brief look at that suggests that you are introducing a syntax error into your Python code because you are not closing your quotes correctly. Your error log will have more detail about where the syntax error is so that you can track down the problem.

I checked it. Its not a syntax error on the script. Only when I load the page, its showing the error on the page.

Its caused by these three lines. when I remove it, everything work perfectly. And these lines I just copied from GS tracking code.

function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-#######-1');

[edited by admin: code formatting]

What error is it showing on the page?

This is the error enter image description here

Here is the error screenshot - https://ibb.co/N258Vbw

Thanks a lot in advance. This is the last error on the error log

2020-05-27 08:00:24,895: File "/home/myname/mysite/flask_app.py", line 382, in adder_page 2020-05-27 08:00:24,895: '''.format(errors=errors)

The line from your error log that you posted above is not the complete traceback, but it does suggest that your problem may be around line 382 in flask_app.py. Read the rest of the exception traceback to see what the actual problem is. It may also be that 382 is not where the original error is but, if it doesn't, the rest of the traceback should indicate where the problem is.

Hi akshin, i'm having the same issue. Did you manage to solve it? thanks a lot

What is the exact error that you are seeing?

Hey giles, once i copy the google analytics tag in the <head>, the error code is Unhandled Exception upon refreshing the page. Then this is what appears in the error log, you can see the screenshot here: https://ibb.co/xSVJ8WQ

thanks for the help!

What was the change to your code? Did it work before?

Hi fijl, everything works perfectly until i add the google analytics tag, highlighted in blue in this screenshot: https://ibb.co/jRttjfk

Hi, it looks like your Python code is interpreting parts in curly braces from js code as part of the template and is looking for dataLayer which is part of js script. You'd probably need to escape those braces in the template.

HA! Thanks a lot pafk, that was exactly it, it all seems to be working now ;)

Excellent, thanks for letting us know!

If anybody referred to this post but didn't get the answer they needed like me, if your error is something along the lines of "failed to load resource net::ERR_BLOCKED_BY_CLIENT", then it may be your ad blocker blocking the javascript tag. All I had to do was disable the ad blocker for my website and the Google Tag Manager connection was established.

That's useful to know -- thanks for posting!

Hi, got a question related to this too. My app is build from one .py file only. No assets folders or anything. How can I include this:

<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-AAAAAAA"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'AAAAAAAA');
</script>

in to my code or file system to make it work please?

Which framework do you use? It should be incorporated into the html page/template, I guess.

It is Dash based dashboard and I included 'styling' code in the same file only.

Maybe this will help then: https://dash.plotly.com/dash-html-components.