Reputation: 121
I'm having trouble getting data on 30-day active users when I query Google Analytics in python. My code is below. My problem is that I need to specify a date or day as dimension. "At least one of ga:nthDay, ga:date, or ga:day must be specified as a dimension to query this metric." I'm just not sure what the proper syntax is for this.
from oauth2client.service_account import ServiceAccountCredentials
from apiclient.discovery import build
import httplib2
#create service credentials
#this is where you'll need your json key
#replace "keys/key.json" with the path to your own json key
key_file_location = 'maypath'
credentials =
ServiceAccountCredentials.from_json_keyfile_name(key_file_location,
['https://www.googleapis.com/auth/analytics.readonly'])
# create a service object you'll later on to create reports
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': 'ga:thisismyid',
'dateRanges': [{'startDate': '2017-10-01', 'endDate': '2017-10-01'}],
'metrics': [{'expression': 'ga:sessions'},
{'expression': 'ga:bounceRate'},
{'expression': 'ga:avgSessionDuration'},
{'expression': 'ga:users'},
{'expression': 'ga:newUsers'},
{'expression': 'ga:sessionsPerUser'},
{'expression': 'ga:30dayUsers'}],
'dimensions': [{"name": "ga:pagePath"},
{"name": "ga:city"}],
'orderBys': [{"fieldName": "ga:sessions", "sortOrder": "DESCENDING"}],
'pageSize': 10
}]
}
).execute()
So, in this example I am asking for a bunch of metrics on one date, including 30-day active users. As is, I hit this error: "Selected dimensions and metrics cannot be queried together," which is presumably because I am not specifying ga:date or ga:day as one of my dimensions. I've tried doing this a couple ways, but my syntax must be off.
So how would I specify ga:day as a dimension to get the number of 30-day users ending on the date I'm pulling data for? Note that if I simply get rid of the request for ga:30dayUsers in the above code, it works fine.
Upvotes: 2
Views: 1968
Reputation: 21
this is a simple case with batch ,and I reference to GA API V4 Dimensions&Metrics
body={
'reportRequests': [
{
'viewId': VIEW_ID,
'dateRanges': [{'startDate': '2018-01-11',
'endDate': '2018-01-18'}],
'metrics': [{'expression': 'ga:28dayUsers'}],
# 'metrics': [{'expression': 'ga:sessions'}]
'dimensions': [{'name': 'ga:date'}]
}]
}
Upvotes: 1