Created
June 12, 2024 19:48
-
-
Save codearachnid/a2172c386f2e1e1cd28256e8c2347d3a to your computer and use it in GitHub Desktop.
Export Universal Analytics from Google Analytics API with pagination due to API limits
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from google.oauth2 import service_account | |
from googleapiclient.discovery import build | |
from datetime import datetime | |
import csv | |
# Set up credentials and analytics service | |
SCOPES = ['https://www.googleapis.com/auth/analytics.readonly'] | |
KEY_FILE_LOCATION = 'path/to/your-credentials-file.json' | |
VIEW_ID = 'your-view-id' | |
credentials = service_account.Credentials.from_service_account_file( | |
KEY_FILE_LOCATION, scopes=SCOPES) | |
analytics = build('analyticsreporting', 'v4', credentials=credentials) | |
def get_report(analytics, view_id, start_date, end_date, page_token=None): | |
return analytics.reports().batchGet( | |
body={ | |
'reportRequests': [ | |
{ | |
'viewId': view_id, | |
'dateRanges': [{'startDate': start_date, 'endDate': end_date}], | |
'metrics': [{'expression': 'ga:sessions'}, {'expression': 'ga:pageviews'}], # Add more metrics if needed | |
'dimensions': [{'name': 'ga:date'}, {'name': 'ga:pagePath'}], # Add more dimensions if needed | |
'pageToken': page_token, | |
'pageSize': 10000 | |
}] | |
} | |
).execute() | |
def save_to_csv(response, filename, is_first_page): | |
with open(filename, 'a', newline='') as csvfile: | |
writer = csv.writer(csvfile) | |
if is_first_page: | |
header = ['Date', 'PagePath', 'Sessions', 'Pageviews'] | |
writer.writerow(header) | |
for report in response.get('reports', []): | |
for row in report.get('data', {}).get('rows', []): | |
dimensions = row.get('dimensions', []) | |
metrics = row.get('metrics', [])[0].get('values', []) | |
writer.writerow(dimensions + metrics) | |
def export_google_analytics_data(year, month): | |
start_date = f"{year}-{month:02d}-01" | |
end_date = f"{year}-{month:02d}-{datetime(year, month, 1).replace(day=28).day}" # Handles month-end correctly | |
page_token = None | |
is_first_page = True | |
filename = f'ga_data_{year}_{month:02d}.csv' | |
while True: | |
response = get_report(analytics, VIEW_ID, start_date, end_date, page_token) | |
save_to_csv(response, filename, is_first_page) | |
page_token = response.get('reports', [])[0].get('nextPageToken') | |
if not page_token: | |
break | |
is_first_page = False | |
print(f"Data for {year}-{month:02d} has been exported to {filename}") | |
# Example usage | |
export_google_analytics_data(2023, 5) # Specify the year and month here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment