I’m working on a group project where we are trying to pull data from Google Analytics using their API to build a database. Unfortunately, we’re having trouble getting the API to connect properly.
Our main issue is needing a View ID for our project, but we can’t seem to create the correct type of account. Each time we try to set up a new Analytics account, it defaults to the new GA4 version (App and Web Properties), which doesn’t provide View IDs.
We’ve even attempted to create new Google accounts, but the problem persists. Can anyone guide us on how to resolve this or how to make the older property type we require?
Below is the Python code we are using in Jupyter notebooks:
# Load necessary libraries
from oauth2client.service_account import ServiceAccountCredentials
from apiclient.discovery import build
import httplib2
import pandas as pd
# Setup authentication
# Remember to rename your JSON key file to 'client_secrets.json' and place it in your working directory
credentials = ServiceAccountCredentials.from_json_keyfile_name('client_secrets.json',
['https://www.googleapis.com/auth/analytics.readonly'])
# Create a service object for Analytics
http = credentials.authorize(httplib2.Http())
service = build('analytics', 'v4', http=http, discoveryServiceUrl=
('https://analyticsreporting.googleapis.com/$discovery/rest'))
response = service.reports().batchGet(
body={
'reportRequests': [
{
'viewId': '987654321', # Add your actual View ID here
'dateRanges': [{'startDate': '30daysAgo', 'endDate': 'today'}],
'metrics': [{'expression': 'ga:userCount'}],
'dimensions': [{"name": "ga:landingPagePath"}],
"filtersExpression":"ga:landingPagePath=~services;ga:landingPagePath!@/admin",
'orderBys': [{"fieldName": "ga:userCount", "sortOrder": "DESCENDING"}],
'pageSize': 100
}
]
}
).execute()
# Initialize empty lists for dimensions and counts
landing_pages = []
user_counts = []
# Extract the data from the response
for report in response.get('reports', []):
headers = report.get('columnHeader', {})
dimension_headers = headers.get('dimensions', [])
metric_headers = headers.get('metricHeader', {}).get('metricHeaderEntries', [])
rows = report.get('data', {}).get('rows', [])
for row in rows:
dimensions = row.get('dimensions', [])
metrics = row.get('metrics', [])
for header, dimension in zip(dimension_headers, dimensions):
landing_pages.append(dimension)
for index, values in enumerate(metrics):
for metric_header, value in zip(metric_headers, values.get('values')):
user_counts.append(int(value))
# Sort the data in reverse order
user_counts.reverse()
landing_pages.reverse()
data_frame = pd.DataFrame()
data_frame["User_Count"] = user_counts
data_frame["Landing_Page"] = landing_pages
data_frame = data_frame[["Landing_Page", "User_Count"]]
print(data_frame)
# Save the results to CSV
data_frame.to_csv("landing_page_data.csv")