将谷歌分析跟踪代码添加到 Dash 应用程序

时间:2021-02-18 20:19:40

标签: python google-analytics plotly-dash

我正在尝试将 Google Analytics 跟踪添加到 Dash Web 应用程序。我查看了其他类似的问题并遵循了建议,但是,我遇到了一个问题。

在我的app.py中,

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>

错误:

<html>
    ^
SyntaxError: invalid syntax

如何在我的 Dash 应用中添加 GA 跟踪代码?

1 个答案:

答案 0 :(得分:0)

下面的代码段对我有用 kms!

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

    gtag('config', 'G-458QGY6CQV');
    </script>
</head>
"""