Skip to content

Commit ee22017

Browse files
committed
Initial commit
0 parents  commit ee22017

File tree

8 files changed

+183
-0
lines changed

8 files changed

+183
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
venv
2+
.idea
3+
example.py
4+
client_secrets.json

LICENSE.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
MIT License
2+
Copyright (c) 2018 YOUR NAME
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
The above copyright notice and this permission notice shall be included in all
10+
copies or substantial portions of the Software.
11+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17+
SOFTWARE.

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# GAPandas4
2+
GAPandas4 is a Python package for querying the Google Analytics Data API for GA4 and displaying the results in a Pandas dataframe. It is the successor to the GAPandas package, which did the same thing for GA3 or Universal Analytics. GAPandas4 is a wrapper around the official Google Analytics Data API package and simplifies imports and queries, requiring far less code.
3+
4+
### Before you start
5+
In order to use GAPandas4 you will first need to create a Google Service Account with access to the Google Analytics Data API and export a client secrets JSON keyfile to use for authentication. You'll also need to add the service account email address as a user on the Google Analytics 4 property you wish to access, and you'll need to note the property ID to use in your queries. There's more information on that here.
6+
7+
### Installation
8+
As this is currently in alpha, there's currently no Pip package, however, you can install the code into your Python environment directly from GitHub using the command below. It will run fine in a Jupyter notebook, a Python IDE, or a Python script.
9+
10+
### Usage
11+
GAPandas4 has been written to allow you to use as little code as possible. Unlike the previous version of GAPandas for Universal Analytics, which used a payload based on a Python dictionary, GAPandas4 now uses a Protobuf (Protocol Buffer) payload as used in the API itself.
12+
13+
Providing there's data in your Google Analytics 4 property, the below query should return a Pandas dataframe of your data.
14+
15+
```python
16+
import gapandas4 as gp
17+
18+
service_account = 'client_secrets.json'
19+
property_id = '123456789'
20+
21+
request = gp.RunReportRequest(
22+
property=f"properties/{property_id}",
23+
dimensions=[
24+
gp.Dimension(name="country"),
25+
gp.Dimension(name="city")
26+
],
27+
metrics=[
28+
gp.Metric(name="activeUsers")
29+
],
30+
date_ranges=[gp.DateRange(start_date="2022-06-01", end_date="2022-06-01")],
31+
)
32+
33+
df = gp.run_report(service_account, request)
34+
print(df.head())
35+
```
36+

gapandas4/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from .gapandas4 import run_report
2+
3+
from google.analytics.data_v1beta.types import DateRange
4+
from google.analytics.data_v1beta.types import Dimension
5+
from google.analytics.data_v1beta.types import Metric
6+
from google.analytics.data_v1beta.types import OrderBy
7+
from google.analytics.data_v1beta.types import Filter
8+
from google.analytics.data_v1beta.types import FilterExpression
9+
from google.analytics.data_v1beta.types import FilterExpressionList
10+
from google.analytics.data_v1beta.types import MetricType
11+
from google.analytics.data_v1beta.types import RunReportRequest
12+

gapandas4/gapandas4.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
"""
2+
GAPandas4
3+
"""
4+
5+
import os
6+
import pandas as pd
7+
from google.analytics.data_v1beta import BetaAnalyticsDataClient
8+
9+
10+
def _get_client(service_account):
11+
"""Create a connection using a service account"""
12+
13+
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = service_account
14+
client = BetaAnalyticsDataClient()
15+
16+
return client
17+
18+
19+
def _get_request(service_account, request):
20+
"""Pass a request to the API and return a response"""
21+
22+
client = _get_client(service_account)
23+
response = client.run_report(request)
24+
return response
25+
26+
27+
def _get_headers(response):
28+
"""Return a Python list of dimension and metric header names from the Protobuf response.
29+
"""
30+
31+
headers = []
32+
33+
for header in response.dimension_headers:
34+
headers.append(header.name)
35+
36+
for header in response.metric_headers:
37+
headers.append(header.name)
38+
39+
return headers
40+
41+
42+
def _get_rows(response):
43+
"""Return a Python list of row value lists from the Protobuf response.
44+
"""
45+
46+
rows = []
47+
48+
for _row in response.rows:
49+
50+
row = []
51+
52+
for dimension in _row.dimension_values:
53+
row.append(dimension.value)
54+
55+
for metric in _row.metric_values:
56+
row.append(metric.value)
57+
58+
rows.append(row)
59+
60+
return rows
61+
62+
63+
def _to_dataframe(response):
64+
"""Returns a Pandas dataframe of results."""
65+
66+
headers = _get_headers(response)
67+
rows = _get_rows(response)
68+
df = pd.DataFrame(rows, columns=headers)
69+
70+
return df
71+
72+
73+
def run_report(service_account, request):
74+
"""Pass a Protobuf request to the GA4 API using runReport and return a Pandas dataframe.
75+
"""
76+
77+
response = _get_request(service_account, request)
78+
df = _to_dataframe(response)
79+
80+
return df
81+

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pandas>=1.2.5
2+
google-analytics-data

setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[metadata]
2+
description-file = README.md

setup.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from setuptools import setup
2+
3+
from os import path
4+
this_directory = path.abspath(path.dirname(__file__))
5+
with open(path.join(this_directory, 'README.md'), encoding='utf-8') as f:
6+
long_description = f.read()
7+
8+
setup(
9+
name='gapandas4',
10+
packages=['gapandas4'],
11+
version='0.001',
12+
license='MIT',
13+
description='GAPandas4 is a Python package for accessing the Google Analytics Data API for GA4 using Pandas',
14+
long_description=long_description,
15+
long_description_content_type='text/markdown',
16+
author='Matt Clarke',
17+
author_email='[email protected]',
18+
url='https://github.com/practicaldatascience/gapandas4',
19+
download_url='https://github.com/practicaldatascience/gapandas4/archive/master.zip',
20+
keywords=['python', 'google analytics', 'ga', 'pandas', 'universal analytics', 'gapandas', 'ga4'],
21+
classifiers=[
22+
'Development Status :: 3 - Alpha',
23+
'Intended Audience :: Developers',
24+
'Topic :: Software Development :: Libraries :: Python Modules',
25+
'License :: OSI Approved :: MIT License',
26+
'Programming Language :: Python :: 3.6',
27+
],
28+
install_requires=['pandas', 'google-analytics-data']
29+
)

0 commit comments

Comments
 (0)