GA多通道漏斗报告Python错误



我已经能够使用reports().batchGet()

在G网站上成功从Google Analytics查询数据。

现在,我必须使用多通道漏斗(MCF)进行同样的操作。

我在stacko上或其他任何地方都能看到的所有内容都是致电service.data().mcf().get()。但是后来我得到了错误 AttributeError: 'Resource' object has no attribute 'service'

我认为这是我构建请求的方式,但是在我不知道我错之前的方式中使用Google apiclient

在这里,我启动此调用的方式的示例:

from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']
KEY_FILE_LOCATION = 'client_secrets.json'
def initialize_analytics_reporting():
    """
    Initializes an Analytics Reporting API V4 service object.
    :return: An authorized Analytics Reporting API V4 service object.
    """
    credentials = ServiceAccountCredentials.from_json_keyfile_name(
        KEY_FILE_LOCATION, SCOPES)
    # Build the service object.
    analytics = build('analyticsreporting', 'v4', credentials=credentials)
    return analytics
def get_report(analytics, next_token):
    """
    Queries the Analytics Reporting API V4.
    Args:
      analytics: An authorized Analytics Reporting API V4 service object.
    Returns:
      The Analytics Reporting API V4 response.
    """
    print(analytics.__dict__)
    if next_token == 0:
        return analytics.data.mcf().get(..)
    [...]

不适合我或其他人:

analytics = build('analytics', 'v3', credentials=credentials)

工作!

您可以使用以下代码绘制数据:

def initialize_mcf_analyticsreporting():
    credentials = ServiceAccountCredentials.from_json_keyfile_name(
        KEY_FILE_LOCATION, SCOPES)
    analytics = build('analytics', 'v3', credentials=credentials)
    mcf_service = analytics.data().mcf()
    return mcf_service
mcf_service = initialize_mcf_analyticsreporting()
report = mcf_service.get(
        start_date=start_date,
        end_date=end_date,
        metrics='mcf:totalConversions,mcf:totalConversionValue',
        ids='ga:xxxxxxx', #Your View ID to replaced by xxxxxxx
        dimensions="mcf:sourceMediumPath,mcf:campaignPath",
        max_results=10000
).execute()

您可以在此处参考Google的文档https://developers.google.com/analytics/devguides/reporting/mcf/v3/mcfdevguide有关其他信息。

最新更新