Unable to Query on GA-v4 API explorer - google-analytics

Below is the page for API v4 to get GA data
https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet
My request :
{
"reportRequests": [
{
"dateRanges": [
{
"startDate": "7daysAgo",
"endDate": "yesterday"
}
],
"metrics": [
{
"expression": "ga:users"
}
]
},
{
"viewId": "ga:123456"
}
]
}
Response :
{
"error": {
"code": 400,
"message": "Parameter viewId must be set.",
"status": "INVALID_ARGUMENT"
}
}
Query Explorer uses the the same ViewID and request parameter and it is fetching the data properly. What I am missing?

You are missing the viewId key in your first ReportRequest object. Having viewId standalone in a separate object doesn't work. Try this code instead:
{
"reportRequests": [
{
"viewId": "ga:123456", //viewId defined within the ReportRequest object itself
"dateRanges": [
{
"startDate": "7daysAgo",
"endDate": "yesterday"
}
],
"metrics": [
{
"expression": "ga:users"
}
]
}
]
}

You are missing a dimension like ga:date. Here is the list with possible dimensions to use in the GA-API: https://developers.google.com/analytics/devguides/reporting/core/dimsmets

Related

Google analytics reporting dimensions & metrics are incompatible

We have custom dimension define in Google Analytics Data API v1Beta for extracting data from Google Analytics GA4 account.
This worked fine till yesterday.
Error:
Please remove customEvent:institution_id to make the request compatible. The request's dimensions & metrics are incompatible. To learn more, see https://ga-dev-tools.web.app/ga4/dimensions-metrics-explorer/
POST - https://analyticsdata.googleapis.com/v1beta/properties/{propertyId}:runReport
Body
{
"dateRanges": [
{
"startDate": "2022-08-29",
"endDate": "2022-12-07"
}
],
"dimensions": [
{
"name": "customEvent:institution_id"
},
{
"name": "pagePathPlusQueryString"
}
],
"dimensionFilter": {
"andGroup": {
"expressions": [
{
"filter": {
"fieldName": "customEvent:institution_id",
"inListFilter": {
"values": [
"47339486-a1e5-47be-abce-e4270af23rte"
]
}
}
},
{
"filter": {
"fieldName": "pagePathPlusQueryString",
"stringFilter": {
"matchType": "PARTIAL_REGEXP",
"value": "/clip/.+",
"caseSensitive": false
}
}
}
]
}
},
"metrics": [
{
"name": "screenPageViews"
}
],
"metricAggregations": [
"TOTAL"
],
"limit": "10000"
}
Dimensions that include the query string like pagePathPlusQueryString are only compatible with a limited set of dimensions & metrics.
This changed was announced 2022-09-13 Schema compatibility changes announcement.
It went live earlier this week. So the cause if the error is that the "customEvent:institution_id" dimension is not compatible with "pagePathPlusQueryString".

How do I query for monthly page views on a particular page using Google Analytics Data API?

Is this the right request to send? And then I guess I need to manually sum the metrics over each date in the months? I can't even get the monthly view on the google analytics UI. It only lets me look at weekly. I am using google analytics v4 property. Docs
POST https://analyticsdata.googleapis.com/v1alpha:runReport
{
"entity": { "propertyId": "XXXX" },
"dateRanges": [{ "startDate": "2020-10-01", "endDate": "<today>" }],
"dimensions": [{ "name": "date" }],
"metrics": [{ "name": "eventCount" }],
"dimensionFilter": {
"andGroup": {
"expressions": [
{
"filter": {
"fieldName": "eventName",
"stringFilter": {
"value": "page_view"
}
}
},
{
"filter": {
"fieldName": "unifiedPageScreen",
"stringFilter": {
"value": "www.mysite.com/page_of_interest"
}
}
}
]
}
},
}
How is unifiedPageScreen populated on the page_view event? Do I need to do that manually?

How to retrieve firebase documents missing a field using runQuery and the IN operator?

This is my http POST requst body...
{
"structuredQuery": {
"select": {
"fields": [
{
"fieldPath": "name"
},
{
"fieldPath": "taxId"
},
{
"fieldPath": "mailingAddress"
}
]
},
"from": [
{
"collectionId": "orgs"
}
],
"where": {
"fieldFilter": {
"field": {
"fieldPath": "orgId"
},
"op": "IN",
"value": {
"arrayValue": {
"values": [
{
"stringValue": ""
},
{
"nullValue": null
}
]
}
}
}
}
}
It fails to return orgs where the orgId field is completely missing from the document. It correctly includes orgs where the orgId field is present and equal to empty string. This is accessing a Cloud Firestore db.
Due to the way Firestore indexes data, it is not possible to query for documents for which a certain field "is completely missing from the document": the field needs to exist in order for the Firestore index to take it into account. More details on the indexing mechanism in the following official video.
You may store an empty value in this field, as you mention in your question.

Google Analytics API V4 - Lookup by URL Query

I am trying to use GA API to check if lead submissions are going through our system correctly.
I need to check if pageview has been logged by GA on a url like below:
*www.mysite.com/leads/?e={{email}}
The e= parameter will be a string variable (email address) I pass to my API request, so basically I am checking to see if there is a logged pageview for the passed email.
I have been looking at the GA docs and think I need to use a filter of some kind but a little unsure how to proceed.
Here is my current code, it is simply checking for all todays PageViews without any filter.
{
"reportRequests": [
{
"viewId": {{View-ID}},
"dateRanges": [
{
"startDate": "today",
"endDate": "today"
}
],
"metrics": [
{
"expression": "ga:pageviews"
}
],
"dimensions":[
{
"name":"ga:date"
}
]
}
]
}
To answer your question in a more general sense, you should use dimension filter clause.
Example:
{
"reportRequests": [
{
"viewId": {{View-ID}},
"dateRanges": [
{
"startDate": "today",
"endDate": "today"
}
],
"metrics": [
{
"expression": "ga:pageviews"
}
],
"dimensions":[
{
"name":"ga:date"
}
],
"dimensionFilterClauses": [
{
"filters": [
{
"dimensionName": "ga:page",
"operator": "PARTIAL",
"expressions": {{email}}
}
]
}
]
}
]
}

Extract member info from Google Analytics

I am trying to get all data from my Google Analytics account to a database using their Reporting API v4. In the below request and response, I see a number in the values attribute. I want to get member specific data for that number? For example, value is 65 for dimension 20130101. I want to get data of those 65 members that accessed my website that day (2013-01-01 - the specified dimension in the request). Any data that GA has will suffice. For example, the Gender of those 65 members, their session Id, age etc.
Google Analytics Reporting API Request:
{
"reportRequests": [
{
"viewId": "345",
"dateRanges": [
{
"startDate": "daysAgo",
"endDate": "yesterday"
}
],
"metrics": [
{
"expression": "ga:users"
}
],
"dimensions":[
{
"name":"ga:date"
}
]
}
]
}
Response
{
"reports": [
{
"columnHeader": {
"dimensions": [
"ga:date"
],
"metricHeader": {
"metricHeaderEntries": [
{
"name": "ga:users",
"type": "INTEGER"
}
]
}
},
"data": {
"rows": [
{
"dimensions": [
"20130101"
],
"metrics": [
{
"values": [
"65"
]
}
]
},
{
"dimensions": [
"20130102"
],
"metrics": [
{
"values": [
"69"
]
}
]
},
{
"dimensions": [
"20130103"
],
"metrics": [
{
"values": [
"48"
]
}
]
}
],
"totals": [
{
"values": [
"490"
]
}
],
"rowCount": 3,
"minimums": [
{
"values": [
"44"
]
}
],
"maximums": [
{
"values": [
"94"
]
}
],
"isDataGolden": true
}
}
]
}
Google Analytics reporting api don't provide single rows reports, that means that all the data that you can collect is gruped by the dimension.
In that case, if you want to retrieve al this data in a single report, you have to implement a custom dimension to a users level to identify each one, one option is send the client id (the _ga cookie) to the platform and add that dimension
Here is a good post of how to implement this
https://www.simoahava.com/analytics/add-clientid-to-custom-dimension-gtag-js/
Have in mind that you can expect others on the reports in that case, due the high cardinality.
https://support.google.com/analytics/answer/1009671?hl=en
For premium users you can export the data via BigQuery
Greetings

Resources