Syntax error : Add GA Tracking code in Dash App

I am trying to add GA Tracking code to dash application. I get an syntax error when I include html code in

app = dash.Dash(....) 

        <!-- Global site tag (gtag.js) - Google Analytics -->
        <script async src=""></script>
            window.dataLayer = window.dataLayer || [];
            function gtag(){dataLayer.push(arguments);}
            gtag('js', new Date());

            gtag('config', 'G-XXXXXXXX');
SyntaxError: invalid syntax is a Python script. The Python interpreter won’t know what to do with HTML if you put it in there.

One alternative option would be to add those script tags to the HTML index template. See the section Customising Dash’s HTML index template in the docs.

1 Like