Google Tag Manager not working as expected in Dash app

I have made a stellar dash app, and want to track the visitors with google tag manager. I have tried a lot of variations based on similar questions, but just cant get it to work as expected. This is my

import dash
from dash import Input, Output, State, html, dcc
import dash_labs as dl
import dash_bootstrap_components as dbc

from navbars import header_bar, footer_bar
from callbacks import get_callbacks

app = dash.Dash(
            __name__, plugins=[dl.plugins.pages],
            meta_tags=[{'name': 'viewport',
                             'content': 'width=device-width, initial-scale=1, maximum-scale=1.0, minimum-scale=0.5'},

app.index_string = """
    <!DOCTYPE html>
<noscript><iframe src=""
            height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>

And the assets/gtag.js:

new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],

I expect to see something like what Plotly made in the app gallery):

But my result is missing the google tag manager script:

I have also tried to append the script with app.index_string, but still does not show as expected.
PS. I have a similar question on stackoverflow.

Hope anyone can help me, as I am getting frustrated after days tryeing every combination of solutions.

Congrats on your stellar dash app and sorry for your frustration with trying to get the custom app.index_string working.

Currently, making a multi-page app with the pages plug-in in dash-labs, it’s not possible to have a custom index_string because it uses the dash-labs custom index_string instead.

The good news is that when pages is integrated with dash, it will work (I’ve verified this in the pull request). This is scheduled to be available in the Dash 2.5 release. Coming Soon! :crossed_fingers:

1 Like