简体   繁体   English

Flask 的谷歌分析不跟踪页面浏览信息

[英]Google analytics for Flask doesn't track the page view information

I've been playing around with GA (Google-Analytics) and I can't get it to track the page view I am triggering an event in.我一直在玩 GA(Google-Analytics),但我无法让它跟踪我触发事件的页面视图。

Background背景

I am not using any fancy 3rd party library for this;我没有为此使用任何花哨的第 3 方库; I only post a json object with the things I need to be tracked, following the steps provided here :我只发布了一个 json object 和我需要跟踪的东西,按照这里提供的步骤:

The connection between my Flask app and their API works, since I get 1 user connected in my GA dashboard while navigating through my website.我的 Flask 应用程序和他们的 API 之间的连接有效,因为我在浏览我的网站时在我的 GA 仪表板中连接了 1 个用户。

The outcome I need to achieve is once I navigate to a route, say 127.0.0.1/projects , I want that route to be shown in my Real-Time/Overview/Top Activate Pages .我需要实现的结果是,一旦我导航到一条路线,比如127.0.0.1/projects ,我希望该路线显示在我的Real-Time/Overview/Top Activate Pages中。

在此处输入图像描述

Code代码

In my utils.py I have a function called track_event which is being imported anytime I need it在我的utils.py ,我有一个名为track_event的 function,我可以随时导入它

''' Google analytics tracking function. '''
def track_event(event, category, action, label=None, value=0):
    data = {
        'v': '1',  # API Version.
        'tid': current_app.config['GA_TRACKING_ID'],  # Tracking ID / Property ID.
        # Anonymous Client Identifier. Ideally, this should be a UUID that
        # is associated with particular user, device, or browser instance.
        'cid': '555',
        't': event,  # Event hit type.
        'ec': category,  # Event category.
        'ea': action,  # Event action.
        'el': label,  # Event label.
        'ev': value,  # Event value, must be an integer
        'ua': 'Opera/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14'
    }

    print(data)

    response = requests.post(
        'https://www.google-analytics.com/collect', data=data)

    print(response.raise_for_status)
    
    # If the request fails, this will raise a RequestException. Depending
    # on your application's needs, this may e a non-error and can be caught by
    # the caller.
    response.raise_for_status()

  • Route that imports the tracking function *导入跟踪的路由 function *
''' Route of dynamically generated post. '''
@bp.route('/projects/post/<post_id>')
def post(post_id):
    track_event(event=f'/projects/post/{post_id}',category='Projects', action=f'post triggered {post_id}')
    # some more code
    return render_template('post.html', title=f'Project {post_id}', project=project, images=images)

By looking at GA collection parameters , the parameter t should be the one I m interested in:通过查看GA collection parameters ,参数t应该是我感兴趣的那个:

Required for all hit types.所有点击类型都需要。 The type of hit.命中的类型。 Must be one of 'pageview', 'screenview', 'event', 'transaction', 'item', 'social', 'exception', 'timing'.必须是“pageview”、“screenview”、“event”、“transaction”、“item”、“social”、“exception”、“timing”之一。

Even with that, nothing shows up in the dashboard aforementioned.即使这样,上述仪表板中也没有任何显示。 Any tips or hints would be highly appreciated任何提示或提示将不胜感激

It might be easier to use the gtag.js client library .使用gtag.js 客户端库可能更容易。

You would put this in the head tag of your global HTML template (swapping in your GA_MEASUREMENT_ID ).您可以将它放在全局 HTML 模板的 head 标签中(交换您的GA_MEASUREMENT_ID )。

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

  gtag('config', 'GA_MEASUREMENT_ID');
</script>

It will automatically capture page views unless you explicitly turn them off by specifying:它将自动捕获页面浏览量,除非您通过指定明确关闭它们:

gtag('config', 'GA_MEASUREMENT_ID', { 'send_page_view': false });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM