简体   繁体   中英

How to add Google Analytics to plotly dash app

I built a site with python dash app and now I want to insert Google Analytics. The google javascript looks like this:

<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-XXXXXX-1', 'auto');
ga('send', 'pageview');
</script>

What I've tried so far:

  1. app.scripts.append_script({'external_url':'https://mywebsite.com/assets/gtag.js'})

  2. Put Google Analytics script under /assets

None of these seem to work, since I don't see any updates in the Google Analytics dashboard.

Should I put the script before the HTML head in dash app? How could I do this?

This is solved now.

I should remove the 'script' in the js file, then put it in assets fold, everything is working right now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM