Reputation: 51
I'm trying to receive data from google analytics to make specific report. This report does not include any metric but only dimensions. The problem is that batchGet requests a metric in order to send a request properly. This is the how the data is requested via Data Studio
As you can see the metric section is empty.
And this is how I run the batchGet request (using python)
analytics.reports().batchGet(
body={
'reportRequests': [
{
'viewId': VIEW_ID,
'dateRanges': [{'startDate': '2022-01-01', 'endDate': '2022-01-01'}],
'metrics': [],
'dimensions': [{'name': 'ga:date'},
{'name': 'ga:transactionId'},
{'name': 'ga:adContent'},
{'name': 'ga:source'},
{'name': 'ga:medium'},
{'name': 'ga:campaign'},
{'name': 'ga:keyword'}]
}]
}).execute()
When I run this code I get an error:
"Selected dimensions and metrics cannot be queried together."
And that's because I ask both dimensions {'name': 'ga:transactionId'},{'name': 'ga:adContent'} in the same request.
How can I request this data and receiving this without any errors, because I know I can see it in Data Studio but I cannot request it via Google Analytics Core Reporting API.
Thanks in advance,
Tom
Upvotes: 1
Views: 776
Reputation: 51
Actually I finally found an solution.
I use 2 different requests with a unique field that will map between the transactionId field and the adContent field.
def get_campaign_report(VIEW_ID, analytics, specific_day):
return analytics.reports().batchGet(
body={
'reportRequests': [
{
'viewId': VIEW_ID,
'dateRanges': [{'startDate': specific_day, 'endDate': specific_day}],
'metrics': [{'expression': 'ga:transactions'}],
'dimensions': [{'name': 'ga:clientID'},
{'name': 'ga:visitLength'},
{'name': 'ga:date'},
{'name': 'ga:source'},
{'name': 'ga:medium'},
{'name': 'ga:campaign'},
{'name': 'ga:adContent'},
{'name': 'ga:keyword'}]
}]
}
).execute()
def get_transaction_id(VIEW_ID, analytics, specific_day):
return analytics.reports().batchGet(
body={
'reportRequests': [
{
'viewId': VIEW_ID,
'dateRanges': [{'startDate': specific_day, 'endDate': specific_day}],
'metrics': [{'expression': 'ga:transactions'}],
'dimensions': [{'name': 'ga:clientID'},
{'name': 'ga:visitLength'},
{'name': 'ga:transactionId'}]
}]
}
).execute()
Then I created dictionary for each request: which the key is [{ga:clientID}{ga:visitLength}] which is pretty unique id and then I could mapping between {ga:transactionId} to {ga:adContent}
Upvotes: 0
Reputation: 116868
Not all dimensions and metrics can be queried together.
Selected dimensions and metrics cannot be queried together.
Google Analytics has different scopes for dimensions and metrics: Hits and Sessions.
So when you build you report and you are calling hits (or 'hit-level metrics'), you must call dimensions only within that scope.
There is no work around for this error other than removing one of the offending items. you can use the Dimension and metric refrence to see which one match.
Upvotes: 0