{"id":15442886,"url":"https://github.com/gaapi4py/gaapi4py","last_synced_at":"2025-04-16T03:45:59.973Z","repository":{"id":35045167,"uuid":"200224798","full_name":"gaapi4py/gaapi4py","owner":"gaapi4py","description":"Google Analytics Reporting API v4 for Python 3","archived":false,"fork":false,"pushed_at":"2023-04-25T04:26:59.000Z","size":28,"stargazers_count":33,"open_issues_count":8,"forks_count":15,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-18T13:16:39.066Z","etag":null,"topics":["google-analytics-api","google-analytics-python-api","python3","reporting-api-v4"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gaapi4py.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-08-02T11:40:53.000Z","updated_at":"2024-07-09T22:38:26.000Z","dependencies_parsed_at":"2024-11-01T13:03:16.995Z","dependency_job_id":null,"html_url":"https://github.com/gaapi4py/gaapi4py","commit_stats":{"total_commits":35,"total_committers":2,"mean_commits":17.5,"dds":0.05714285714285716,"last_synced_commit":"60a05e379d640d05f7c61089becc228547dfb722"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gaapi4py%2Fgaapi4py","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gaapi4py%2Fgaapi4py/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gaapi4py%2Fgaapi4py/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gaapi4py%2Fgaapi4py/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gaapi4py","download_url":"https://codeload.github.com/gaapi4py/gaapi4py/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249192253,"owners_count":21227753,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["google-analytics-api","google-analytics-python-api","python3","reporting-api-v4"],"created_at":"2024-10-01T19:31:12.562Z","updated_at":"2025-04-16T03:45:59.919Z","avatar_url":"https://github.com/gaapi4py.png","language":"Python","readme":"# gaapi4py\n\nGoogle Analytics Reporting API v4 for Python 3\n\n## Prerequisites\n\nTo use this library, you need to have a project in Google Cloud Platform and a service account key that has access to Google Analytics account you want to get data from.\n\n## Quick Start\n\n```python\nfrom gaapi4py import GAClient\n# if GOOGLE_APPLICATION_CREDENTIALS is set:\nc = GAClient() \n# or you may specify keyfile path:\nc = GAClient(json_keyfile=\"path/to/keyfile.json\")\n\n\nrequest_body = {\n    'view_id': '123456789',\n    'start_date': '2019-01-01',\n    'end_date': '2019-01-31',\n    'dimensions': {\n        'ga:sourceMedium',\n        'ga:date'\n    },\n    'metrics': {\n        'ga:sessions'\n    },\n    'filter': 'ga:sourceMedium==google / organic' # optional filter clause\n}\n\nresponse = c.get_all_data(request_body)\n\nresponse['info'] # sampling and \"golden\" metadata\n\nresponse['data'] # Pandas dataframe that contains data from GA\n```\n\nIf you want to make many requests to a speficic view or with specific dateranges, you can set date ranges for all future requests:\n\n```python\n# Pass arguments to class init\nc = GAClient(view_id=\"123456789\", start_date=\"2019-09-01\", end_date=\"2019-09-07\") \n# or use methods to overwrite viewID or dateranges\nc.set_view_id('123456789')\nc.set_dateranges('2019-01-01', '2019-01-31')\n\nrequest_body_1 = {\n    'dimensions': {\n        'ga:sourceMedium',\n        'ga:date'\n    },\n    'metrics': {\n        'ga:sessions'\n    }\n}\n\nrequest_body_2 = {\n    'dimensions': {\n        'ga:deviceCategory',\n        'ga:date'\n    },\n    'metrics': {\n        'ga:sessions'\n    }\n}\n\nresponse_1 = c.get_all_data(request_body_1)\nresponse_2 = c.get_all_data(request_body_2)\n```\n\n## Avoid sampling by taking data day-by-day\n\n\u003eImportant! Google Analytics reporting API has a limit of maximum 100 requests per 100 seconds. If you want to iterate over large period of days, you might consider adding `time.sleep(1)` at the end of the loop to avoid reaching this limit.\n\n```python\nfrom datetime import date, timedelta\nfrom time import sleep\n\nimport pandas as pd\nfrom gaapi4py import GAClient\n\nc = GAClient(view_id='123456789')\n\nstart_date = date(2019,7,1)\nend_date = date(2019,7,14)\n\ndf_list = []\niter_date = start_date\nwhile iter_date \u003c= end_date:\n    c.set_dateranges(iter_date, iter_date)\n    response = c.get_all_data({\n        'dimensions': {\n            'ga:sourceMedium',\n            'ga:deviceCategory'\n        },\n        'metrics': {\n            'ga:sessions'\n        }\n    })\n    df = response['data']\n    df['date'] = iter_date\n    df_list.append(response['data'])\n    iter_date = iter_date + timedelta(days=1)\n    time.sleep(1)\n\nall_data = pd.concat(df_list, ignore_index=True)\n\n```\n\n## Avoid \"maximum 7 dimensions\" restriction\n\nIf you store sessionId and/or hitId as custom dimensions ([Example implementation on Simo Ahava's blog](https://www.simoahava.com/analytics/improve-data-collection-with-four-custom-dimensions/)), you can circumvent restriction on maximum number of dimensions and metrics in one report. Example below:\n\n\u003e If sampling starts to appear, try to break the set of dimensions into smaller parts and run queries on them.\n\n```python\none_day = date(2019,7,1)\nc.set_dateranges(one_day, one_day)\n\nSESSION_ID_CD_INDEX = '2'\nHIT_ID_CD_INDEX = '5'\n\nsession_id = 'dimension' + SESSION_ID_CD_INDEX\nhit_id = 'dimension' + HIT_ID_CD_INDEX\n\n\n#Get session scope data\nresponse_1 = c.get_all_data({\n    'dimensions': {\n        'ga:' + session_id,\n        'ga:sourceMedium',\n        'ga:campaign',\n        'ga:keyword',\n        'ga:adContent',\n        'ga:userType',\n        'ga:deviceCategory'\n    },\n    'metrics': {\n        'ga:sessions'\n    }\n})\n\nresponse2 = c.get_all_data({\n    'dimensions': {\n        'ga:' + session_id,\n        'ga:landingPagePath',\n        'ga:secondPagePath',\n        'ga:exitPagePath',\n        'ga:pageDepth',\n        'ga:daysSinceLastSession',\n        'ga:sessionCount'\n    },\n    'metrics': {\n        'ga:hits',\n        'ga:totalEvents',\n        'ga:bounces',\n        'ga:sessionDuration'\n    }\n})\n\nall_data = response_1['data'].merge(response2['data'], on=session_id, how='left')\n\nall_data.rename(index=str, columns={\n    session_id: 'session_id'\n}, inplace=True)\n\nall_data.head()\n\n# Get hit scope data\nhits_response_1 = c.get_all_data({\n    'dimensions': {\n        'ga:' + session_id,\n        'ga:' + hit_id,\n        'ga:pagePath',\n        'ga:previousPagePath',\n        'ga:dateHourMinute'\n    },\n    'metrics': {\n        'ga:hits',\n        'ga:totalEvents',\n        'ga:pageviews'\n    }\n})\n\nhits_response_2 = c.get_all_data({\n    'dimensions': {\n        'ga:' + session_id,\n        'ga:' + hit_id,\n        'ga:eventCategory',\n        'ga:eventAction',\n        'ga:eventLabel'\n    },\n    'metrics': {\n        'ga:totalEvents'\n    }\n})\n\nall_hits_data = hits_response_1['data'].merge(hits_response_2['data'],\n                                              on=[session_id, hit_id],\n                                              how='left')\nall_hits_data.rename(index=str, columns={\n    session_id: 'session_id',\n    hit_id: 'hit_id'\n}, inplace=True)\n\nall_hits_data.head()\n\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgaapi4py%2Fgaapi4py","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgaapi4py%2Fgaapi4py","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgaapi4py%2Fgaapi4py/lists"}