I am trying to hit the Google Analytics v4 API to get the number of events when my Event Action = "myStart". Here is my reportRequest for this value:
"reportRequests": [
{
"viewId": VIEW_ID,
"dateRanges": [{
"startDate": firstDay.format("YYYY-MM-DD"),
"endDate": lastDay.format("YYYY-MM-DD")
}],
"metrics": [{"expression": "ga:eventValue"}],
"metricFilterClauses": [{
"filters": [{
"metricName": "ga:eventAction",
"operator": "EQUAL",
"comparisonValue": "myStart"
}]
}]
}
The response I get is:
code: 400
message: 'Value for field request.metricFilterClauses.filters.comparisonValue = myStart is not valid.'
status: 'INVALID_ARGUMENT'
It would appear that 'myValue' is not what I should send, but the Google Analytics v4 documentation is horrible and doesn't give a solid example or explanation of what this value should be.
How can I get the number of eventAction = myStart events?
ga:eventAction is a dimension, so you should use a dimensionFilterClause not metricFilterClauses.
You can tell which ones are metrics and which ones are dimensions by looking into the Dimensions and Metrics Explorer.
I didn't test but I guess it will look something like this:
"reportRequests": [
{
"viewId": VIEW_ID,
"dateRanges": [{
"startDate": firstDay.format("YYYY-MM-DD"),
"endDate": lastDay.format("YYYY-MM-DD")
}],
"metrics": [{"expression": "ga:eventValue"}],
"dimensionFilterClauses": [{
"filters": [{
"dimensionName": "ga:eventAction",
"operator": "EXACT",
"expressions": ["myStart"]
}]
}]
}
Related
I want to fetch count of active users on my website from GA4 API, but I want to exclude users that visited pages with "/blog/" path in them, I can do this easily on explore section of GA4, where my metric is ActiveUsers and dimension is pagePath, then I set the filter on pagePath and exclude blog pages with Not Contain filter.
but using GA4 API, using NotExpression does not works.
https://analyticsdata.googleapis.com/v1beta/properties/$MyID:runReport
Here's my request body:
{
"dateRanges": [{ "startDate": "yesterday", "endDate": "yesterday" }],
"dimensions": [{ "name": "pagePath" }],
"metrics": [{ "name": "activeUsers" }],
"dimensionFilter": {
"notExpression": {
"filter": {
"fieldName": "pagePath",
"stringFilter": {
"matchType": "CONTAINS",
"value": "/blog/"
}
}
}
}
}
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?
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
I want to make my own cohorts in google analytics. Can i specify my own metrics in metrics list. Default metrics are like :
transactions
transactions per user
revenue
revenue per user
If yes, how to do so? If no, then how to log data for these predefined metrics?
Analytics Reporting API V4:
You can use the Analytics Reporting API V4 to construct cohort requests
For example:
POST https://analyticsreporting.googleapis.com/v4/reports:batchGet
{
"reportRequests":
[
{
"viewId": "XXXX",
"dimensions":
[
{
"name": "ga:cohort"
},
{
"name": "ga:cohortNthDay"
}
],
"metrics":
[
{
"expression": "ga:cohortActiveUsers"
},
{
"expression": "ga:transactions"
}
],
"cohortGroup":
{
"cohorts":
[
{
"name": "cohort 1",
"type": "FIRST_VISIT_DATE",
"dateRange":
{
"startDate": "2015-08-01",
"endDate": "2015-08-01"
}
},
{
"name": "cohort 2",
"type": "FIRST_VISIT_DATE",
"dateRange":
{
"startDate": "2015-07-01",
"endDate": "2015-07-01"
}
}
]
}
}
]
}
See the API explorer for an example.
Not all dimensions and metrics can be queried together. For example you will not be able to query for ga:transactionsPerUser and ga:cohortActiveUsers. See the Dimensions and metric explorer to see what dimensions can be queried together.
I'm trying to make calls the Analytics Reporting API V4 and keep getting back unspecific error messages when trying to use certain dimensions and metrics. For example, I consistently get
{
"error": {
"code": 400,
"message": "Unknown dimension(s): ga:acquisitionTrafficChannel",
"status": "INVALID_ARGUMENT"
}
}
when passing ga:acquisitionTrafficChannel, despite it being documented as a valid dimension. Similarly, I get
{
"error": {
"code": 400,
"message": "Selected dimensions and metrics cannot be queried together.",
"status": "INVALID_ARGUMENT"
}
}
when passing ga:acquisitionSourceMedium (documented here), even when not passing any metrics whatsoever.
Are the docs out of date? Is there some documentation elsewhere about valid combinations of dimensions and metrics?
All the Lifetime Value reports and thus ga:acquisition... dimensions are only valid for App views not web views.
Secondly the cohort/LTV dimensions can only be queried in within a cohort requests for example:
POST https://analyticsreporting.googleapis.com/v4/reports:batchGet
{
"reportRequests": [
{
"viewId": "XXXX",
"dimensions": [
{
"name": "ga:cohort"
},
{
"name": "ga:acquisitionTrafficChannel"
}
],
"metrics": [
{
"expression": "ga:cohortSessionsPerUser"
}
],
"cohortGroup": {
"cohorts": [
{
"name": "cohort 1",
"type": "FIRST_VISIT_DATE",
"dateRange": {
"startDate": "2015-08-01",
"endDate": "2015-09-01"
}
},
{
"name": "cohort 2",
"type": "FIRST_VISIT_DATE",
"dateRange": {
"startDate": "2015-07-01",
"endDate": "2015-08-01"
}
}
],
"lifetimeValue": true
}
}
]
}
The error messages should probably be a bit clearer.
I ran into this problem as well. When I was in the Google Analytics Dashboard, I clicked on Acquisition->All Traffic->Channels and was fooled into thinking that I needed to combine the ga:acquisitionMedium dimension and ga:newUsers metric together.
When I clicked on ga:acquisitionMedium, it said that combining it with ga:newUsers was valid, despite the error that you mentioned in your question! In reality, I just needed to combine ga:medium and ga:newUsers together.
I know this is not the exact query that you were doing, but here is an example of how I queried New Users count where the dimension channel equaled "organic" (note that I am forming the JSON request with Javascript and then using JSON.stringify(req) to send it):
var req = {
reportRequests: [{
viewId: '<Your Google Analytics view ID>',
dimensions: [{ name: 'ga:medium' }],
dimensionFilterClauses: [{
filters: [{
dimensionName: 'ga:medium',
operator: 'EXACT',
expressions: ['organic']
}]
}],
dateRanges: [{ startDate: '2019-11-01', endDate: '2019-11-30' }],
metrics: [{ expression: "ga:newUsers" }]
}]
};
The above query returns 5,654, which is the same as seen in the "Acquisition" section of Google Analytics.
I definitely think the documentation and error message around this could be improved.