I'm trying to fetch user data from google analytics using the following google python api:
https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-py
In the example shown, there is the function 'get_report' looks like this:
def get_report(analytics):
"""Queries the Analytics Reporting API V4.
Args:
analytics: An authorized Analytics Reporting API V4 service object.
Returns:
The Analytics Reporting API V4 response.
"""
return analytics.reports().batchGet(
body={
'reportRequests': [
{
'viewId': VIEW_ID,
'dateRanges': [{'startDate': '7daysAgo', 'endDate': 'today'}],
'metrics': [{'expression': 'ga:sessions'}],
'dimensions': [{'name': 'ga:country'}]
}]
}
).execute()
This syntax works well but in order to get user data I think I have to stick to the following manual:
https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/userActivity/search
To make it even clearer I wanted to run the example from here:
https://developers.google.com/analytics/devguides/reporting/core/v4/user-reporting
The example looks like this:
{
"viewId": "9999999",
"user": {
"type": "CLIENT_ID",
"userId": "1034600000.76425000000"
},
"dateRange": {
"startDate": "2018-01-01",
"endDate": "2018-12-31",
}
}
But this example does not state how to define the function "get_report" from above. From what I understand the argument 'analysis' used in 'get_reports' has a method or class called 'userActivity' which it has. But according to the the second link from above it should also have a method called 'search' which it doesn't have! So how do I get/access the method 'search'? Does it have to be initialized when initiliazing the class 'userActivity'?
At the moment my syntax looks like this:
def get_report(analytics):
"""Queries the Analytics Reporting API V4.
Args:
analytics: An authorized Analytics Reporting API V4 service object.
Returns:
The Analytics Reporting API V4 response.
"""
return analytics.userActivity().search(
{
"viewId": VIEW_ID,
"user": {
"type": "CLIENT_ID",
"userId": "310383817.1547668323"
},
"dateRange": {
"startDate": "2019-01-30",
"endDate": "2019-02-01",
}
}
).execute()
But this syntax does not work! The error message says:
method() takes 1 positional argument but 2 were given
Thank you for your help!
I just found the answer ...
You just have to put the arguments of the search method into a variable called body. It should look like this:
def get_report(analytics):
"""Queries the Analytics Reporting API V4.
Args:
analytics: An authorized Analytics Reporting API V4 service object.
Returns:
The Analytics Reporting API V4 response.
"""
return analytics.userActivity().search(
body = {
"viewId": VIEW_ID,
"user": {
"type": "CLIENT_ID",
"userId": "310383817.1547668323"
},
"dateRange": {
"startDate": "2019-01-30",
"endDate": "2019-02-01",
}
}
).execute()
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