Reporting API v4非常慢

时间:2019-03-13 14:49:13

标签: python google-analytics google-reporting-api

我正在使用Reporting API v4检索有关网站的信息并在页面上共享它们。我正在执行的请求功能正常,但速度很慢,我进行了大量搜索以尝试使其更快。我尝试了这些:“ https://developers.google.com/analytics/devguides/reporting/core/v3/performance

我的请求仍然非常缓慢。我没有达到配额。我试图更改采样级别。我试图更改页面大小和开始日期。

我将此文件导入另一个文件并使用最后两个功能

这是我的代码:

# coding=utf-8



from apiclient.discovery import build

from oauth2client.service_account import ServiceAccountCredentials



SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']

KEY_FILE_LOCATION = '/srv/www/donneesquebec/dev/opendatadev2/apps/wordpress/wp-content/themes/donnees-quebec/js/vaulted-splice-233616-afe0b144ebef.json'

VIEW_ID = '111545477'

START_DATE = '2016-01-01'

END_DATE = 'today'

PAGE_SIZE = 10





def initialize_analyticsreporting():

    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_dataset_views(analytics, dataset_path):

    return analytics.reports().batchGet(

        body={

            'reportRequests': [

                {

                    'viewId': VIEW_ID,

                    'dateRanges': [{'startDate': START_DATE, 'endDate': END_DATE}],

                    'metrics': [{'expression': 'ga:pageViews'}],

                    'dimensions': [{'name': 'ga:pagePath'}],

                    'orderBys': [

                        {

                            "fieldName": "ga:pageViews",

                            "sortOrder": "DESCENDING"

                        }

                    ],

                    'pageSize': PAGE_SIZE,

                    "dimensionFilterClauses": [{

                        "filters": [{

                            "dimensionName": "ga:pagePath",

                            "operator": "EXACT",

                            "expressions": [dataset_path]

                        }]}

                    ],

                }]

        }

    ).execute()





def get_report_dataset_event(analytics, event, dataset_name):

    return analytics.reports().batchGet(

        body={

            'reportRequests': [

                {

                    'viewId': VIEW_ID,

                    'dateRanges': [{'startDate': START_DATE, 'endDate': END_DATE}],

                    'metrics': [{'expression': 'ga:totalEvents'}],

                    'dimensions': [{'name': 'ga:eventLabel'}, {'name': 'ga:eventAction'}],

                    'orderBys': [

                        {

                            "fieldName": "ga:eventLabel",

                            "sortOrder": "DESCENDING"

                        }

                    ],

                    'pageSize': PAGE_SIZE,

                    "dimensionFilterClauses": [{

                        "filters": [{

                            "dimensionName": "ga:eventAction",

                            "operator": "EXACT",

                            "expressions": [event]

                        }]},

                        {

                            "filters": [{

                                "dimensionName": "ga:eventLabel",

                                "operator": "BEGINS_WITH",

                                "expressions": [dataset_name]

                            }]}

                    ],

                }]

        }

    ).execute()



def total_totalEvents(response):

    for report in response.get('reports', []):

        columnHeader = report.get('columnHeader', {})

        metricHeaders = columnHeader.get('metricHeader', {}).get('metricHeaderEntries', [])

        totalEvents = 0



        for row in report.get('data', {}).get('rows', []):

            dateRangeValues = row.get('metrics', [])



            for i, values in enumerate(dateRangeValues):

                for metricHeader, value in zip(metricHeaders, values.get('values')):

                    totalEvents += int(value)



    return totalEvents





def totalViews(response):

    for report in response.get('reports', []):

        columnHeader = report.get('columnHeader', {})

        metricHeaders = columnHeader.get('metricHeader', {}).get('metricHeaderEntries', [])

        totalViews = 0



        for row in report.get('data', {}).get('rows', []):

            dateRangeValues = row.get('metrics', [])



            for i, values in enumerate(dateRangeValues):

                for metricHeader, value in zip(metricHeaders, values.get('values')):

                    totalViews = int(value)



    return totalViews





def get_total_views_of_pagepath(pagepath):

    analytics = initialize_analyticsreporting()

    response = get_report_dataset_views(analytics, pagepath)

    return totalViews(response)





def get_total_event_of_dataset(event, dataset_name):

    analytics = initialize_analyticsreporting()

    response = get_report_dataset_event(analytics, event, dataset_name)

    return total_totalEvents(response)

0 个答案:

没有答案