使用Pandas的替代Google Analytics IO

use*_*531 2 google-analytics pandas

pandas 0.17.1版本已经对pandas.io.ga模块进行了折旧.

现在使用谷歌分析与熊猫有什么替代方案?有可靠的图书馆现在可以使用吗?

小智 7

Google2Pandas模块的建立是为了解决这个特定的问题.没有什么花哨的,只是做它在盒子上说的.

V3:

from google2pandas import GoogleAnalyticsQuery

query = {\
    'ids'           : <valid_ids>,
    'metrics'       : 'pageviews',
    'dimensions'    : ['date', 'pagePath', 'browser'],
    'filters'       : ['pagePath=~iPhone', 'and', 'browser=~Firefox'],
    'start_date'    : '8daysAgo',
    'max_results'   : 10}

conn = GoogleAnalyticsQuery(secrets='client_secrets_v3.json',
                            token_file_name='analytics.dat')
df, metadata = conn.execute_query(**query)
Run Code Online (Sandbox Code Playgroud)

V4:

from google2pandas import GoogleAnalyticsQueryV4

query = {
    'reportRequests': [{
        'viewId' : <valid_ids>,

        'dateRanges': [{
            'startDate' : '8daysAgo',
            'endDate'   : 'today'}],

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

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

        'dimensionFilterClauses' : [{
            'operator' : 'AND',
            'filters'  : [
                {'dimensionName' : 'ga:browser',
                 'operator' : 'REGEXP',
                 'expressions' : ['Firefox']},

                {'dimensionName' : 'ga:pagePath',
                 'operator' : 'REGEXP',
                 'expressions' : ['iPhone']}]
        }]
    }]
}


conn = GoogleAnalyticsQueryV4(secrets='client_secrets_v4.json')
df = conn.execute_query(query)
Run Code Online (Sandbox Code Playgroud)