Google Analytics分析用户ID报告的维度和指标

问题描述

我需要使用每个用户用户ID报告数据填充数据库(我要引用的报告可以在Google Analytics(分析)>报告>用户资源管理器(>单击用户ID)中找到)。

我正在查看文档,但是似乎找不到任何可用于实现此目的的维度或指标:https://ga-dev-tools.appspot.com/dimensions-metrics-explorer/?authuser=1

我希望我有一些代码示例,但是另一种方法是运行此脚本(为简单起见,这里将不包含所有其他功能

def get_user_Activity(analytics,VIEW_ID,user_id,start_date,end_date):
    # Use the Analytics Service Object to query the Analytics Reporting API V4.
    return analytics.userActivity().search(
        body={
            "viewId": VIEW_ID,"user": {
                "type": "USER_ID","userId": user_id
            },"daterange": {
                "startDate": start_date,"endDate": end_date
            },"activityTypes": [
                "PAGEVIEW","EVENT"
            ]
        }
    ).execute()

这将为每个用户ID生成此字典:

{'sessions': [{'sessionId': '1579xxxx','deviceCategory': 'desktop','platform': 'Windows','dataSource': 'web','activiti es': [{'activityTime': '2020-01-22T12:48:20.971410Z',source': '(direct)','medium': '(none)','channelGrouping': 'Direct','campaign': '(not set)','keyword': '(not set)','hostname': 'example.com','landingPagePath': '/somelandingpage','activityType': 'PAGEVIEW','customDimension': [{'index': 1}],'pageview': {'pagePath': '/some/page','pageTitle': 'SOME Title'}},{'activityTime': '2020-01-22T12:48:20.970754Z','source': '(direct)','pageview': {'pagePath': '/some/other/path','pageTitle': 'SomeTitle'}},...
                              ..................
                              etc ..............

方法的麻烦之处在于,我不得不去计算自己感兴趣的大多数指标,相反,我宁愿只收集指标并填充数据库

如果可以提供所需的“ ga:dimension(s)”和“ ga:metric(s)”,将不胜感激。

解决方法

Google Core Reporting API不提供对userId级别报告的内置支持,因此您必须根据Activity API响应中可用的内容来计算所有必要的指标。 唯一的选择是使用与用户ID相同值的用户级自定义维度。但是,这仅在您预先实施了此跟踪的情况下有效。