如何将google analytics(GTAG)添加到我的python dash应用程序中?

2024-09-30 05:23:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个gtag.js,其中包含我的跟踪代码

谷歌说要把这个添加到我的顶部,我怎么能为我的dash应用程序点上他的

我尝试将其添加为/assets下的gtag.js,但没有成功

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

  gtag('config', 'UA-******-1');
</script>


Tags: 代码应用程序taggooglejsscriptsitewindow
1条回答
网友
1楼 · 发布于 2024-09-30 05:23:25

您应至少通过两种方式完成此任务:

  1. 重写索引字符串:

    The following was copied from the docs and updated with the question's JavaScript.

import dash
import dash_html_components as html

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

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

            gtag('config', 'UA-******-1');
        </script>
        {%metas%}
        <title>{%title%}</title>
        {%favicon%}
        {%css%}
    </head>
    <body>
        <div>My Custom header</div>
        {%app_entry%}
        <footer>
            {%config%}
            {%scripts%}
            {%renderer%}
        </footer>
        <div>My Custom footer</div>
    </body>
</html>
'''
  1. 重写Dash.interpolate_index方法:
import dash
import dash_html_components as html


class CustomDash(dash.Dash):
    def interpolate_index(self, **kwargs):
        # Inspect the arguments by printing them
        print(kwargs)
        return '''
        <!DOCTYPE html>
        <html>
            <head>
                <title>My App</title>
                <!  Global site tag (gtag.js) - Google Analytics  >
                <script async src="https://www.googletagmanager.com/gtag/js?id=UA-*****-1"></script>
                <script>
                    window.dataLayer = window.dataLayer || [];
                    function gtag(){dataLayer.push(arguments);}
                    gtag('js', new Date());

                    gtag('config', 'UA-******-1');
                </script>
            </head>
            <body>

                <div id="custom-header">My custom header</div>
                {app_entry}
                {config}
                {scripts}
                {renderer}
                <div id="custom-footer">My custom footer</div>
            </body>
        </html>
        '''.format(
            app_entry=kwargs['app_entry'],
            config=kwargs['config'],
            scripts=kwargs['scripts'],
            renderer=kwargs['renderer'])

app = CustomDash()

请参见Plotly论坛中的the docssislvacl's answer了解更多信息

相关问题 更多 >

    热门问题