so I am currently working on a group project where we are using Google Analytics to pull data, and need to incorporate an API with Google Analytics to use for a database we are creating. We ran into an issue where we could not get the API to properly work. We require a View ID, but for the life of us, we can not figure out how to create an account with a Web Property only, because it simply does not give us an option to create a Web Property. Anytime we try to create a new account, it puts us into the beta version of Analytics for App and Web Properties, which does not give you a View ID.
We tried creating a new Google account as well, and this worked to no avail.
Attached below is our code, if anyone can help us bypass this situation, or tell us what we need to do to create a Web Property only, it would be greatly appreciated!
WE ARE CODING IN PYTHON AND ARE USING JUPYTER
#Load Libraries
from oauth2client.service_account import ServiceAccountCredentials
from apiclient.discovery import build
import httplib2
import pandas as pd
#Create service credentials
#Rename your JSON key to client_secrets.json and save it to your working folder
credentials = ServiceAccountCredentials.from_json_keyfile_name('client_secrets.json',
['https://www.googleapis.com/auth/analytics.readonly'])
#Create a service object
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': '261713611', #Add View ID from GA
'dateRanges': [{'startDate': '30daysAgo', 'endDate': 'today'}],
'metrics': [{'expression': 'ga:sessions'}],
'dimensions': [{"name": "ga:pagePath"}], #Get Pages
"filtersExpression":"ga:pagePath=~products;ga:pagePath!@/translate", #Filter by condition "containing products"
'orderBys': [{"fieldName": "ga:sessions", "sortOrder": "DESCENDING"}],
'pageSize': 100
}]
}
).execute()
#create two empty lists that will hold our dimentions and sessions data
dim = []
val = []
#Extract Data
for report in response.get('reports', []):
columnHeader = report.get('columnHeader', {})
dimensionHeaders = columnHeader.get('dimensions', [])
metricHeaders = columnHeader.get('metricHeader', {}).get('metricHeaderEntries', [])
rows = report.get('data', {}).get('rows', [])
for row in rows:
dimensions = row.get('dimensions', [])
dateRangeValues = row.get('metrics', [])
for header, dimension in zip(dimensionHeaders, dimensions):
dim.append(dimension)
for i, values in enumerate(dateRangeValues):
for metricHeader, value in zip(metricHeaders, values.get('values')):
val.append(int(value))
#Sort Data
val.reverse()
dim.reverse()
df = pd.DataFrame()
df["Sessions"]=val
df["pagePath"]=dim
df=df[["pagePath","Sessions"]]
df
#Export to CSV
df.to_csv("page_by_session.csv")
The Google Analytics Data API v1 can create reports for GA4 Properties. See the developer's site. The Google Analytics Reporting API v4 can create reports for Universal Analytics (GA3) Views (reference). The code you have written is using the Google Analytics Reporting v4.
So you have two options:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With