# Tracking application analytics with google analytics

**URL:** <https://community.plotly.com/t/tracking-application-analytics-with-google-analytics/38946>\
**Category:** Dash Python\
**Created:** [May 5, 2020, 5:23pm UTC](https://community.plotly.com/t/tracking-application-analytics-with-google-analytics/38946 "2020-05-05T17:23:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![howardmf](https://avatars.discourse-cdn.com/v4/letter/h/8baadc/32.png) [@howardmf](https://community.plotly.com/u/howardmf)\
**Post date:** [May 5, 2020, 5:23pm UTC](https://community.plotly.com/t/tracking-application-analytics-with-google-analytics/38946/1 "2020-05-05T17:23:54Z")

</div>

Hey everyone!

I’m looking for a way to track how many people are accessing my dash app and and added bonus would be to have their country as well. Any thoughts on how I could implement this? Google analytics would be a great tool but I am unsure on how to implement it.\

Thanks for the help,

Morgan

---

<div class="post-metadata">

**Author:** ![sislvacl](https://avatars.discourse-cdn.com/v4/letter/s/ecccb3/32.png) [@sislvacl](https://community.plotly.com/u/sislvacl)\
**Post date:** [May 29, 2020, 9:13pm UTC](https://community.plotly.com/t/tracking-application-analytics-with-google-analytics/38946/2 "2020-05-29T21:13:17Z")

</div>

This worked for me

```auto
app = dash.Dash(...)

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-999999-1"></script>
        <script>
            window.dataLayer = window.dataLayer || [];
            function gtag(){dataLayer.push(arguments);}
            gtag('js', new Date());

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

```

You will need your own UA number instead of 999999-1.

This particular setup includes anonymization of IP addresses.

---

<div class="post-metadata">

**Author:** ![keval](https://avatars.discourse-cdn.com/v4/letter/k/ec9cab/32.png) [@keval](https://community.plotly.com/u/keval)\
**Post date:** [February 18, 2021, 8:22pm UTC](https://community.plotly.com/t/tracking-application-analytics-with-google-analytics/38946/4 "2021-02-18T20:22:57Z")

</div>

This didn’t work for me. I get an syntax error when I write `html` code in `app.py`

```auto
app = dash.Dash(....) 

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

            gtag('config', 'G-XXXXXXXX');
        </script>
    </head>
</html>

```

```auto
<html>
    ^
SyntaxError: invalid syntax

```

---

<div class="post-metadata">

**Author:** ![sislvacl](https://avatars.discourse-cdn.com/v4/letter/s/ecccb3/32.png) [@sislvacl](https://community.plotly.com/u/sislvacl)\
**Post date:** [February 18, 2021, 9:03pm UTC](https://community.plotly.com/t/tracking-application-analytics-with-google-analytics/38946/5 "2021-02-18T21:03:53Z")

</div>

Do you assign it to `app.index_string`?

> [@sislvacl](#):
>
> ```auto
> app.index_string = """<!DOCTYPE html>
> <html>
> ...
> </html>
> """
> 
> ```

---

<div class="post-metadata">

**Author:** ![keval](https://avatars.discourse-cdn.com/v4/letter/k/ec9cab/32.png) [@keval](https://community.plotly.com/u/keval)\
**Post date:** [February 19, 2021, 5:16am UTC](https://community.plotly.com/t/tracking-application-analytics-with-google-analytics/38946/6 "2021-02-19T05:16:34Z")

</div>

Aha, I didn’t. Just saw the docs: [Adding CSS & JS and Overriding the Page-Load Template | Dash for Python Documentation | Plotly](https://dash.plotly.com/external-resources)
