Exclude certain page path from count of active users GA4 API - google-analytics

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/"
}
}
}
}
}

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".

Discrepancy between UI and API for transactions/purchase events for GA4

My goal is to pull in purchase transaction count and transaction revenue from a client, segmented by Google Ads campaign ID. My current query looks like the following:
{
"propertyId": "*********",
"query": {
"dateRanges": [
{
"startDate": "2022-09-30",
"endDate": "2022-10-06"
}
],
"dimensions": [
{
"name": "googleAdsCampaignId"
},
{
"name": "googleAdsCampaignName"
}
],
"metrics": [
{
"name": "advertiserAdClicks"
},
{
"name": "advertiserAdCost"
},
{
"name": "transactions"
},
{
"name": "purchaseRevenue"
}
],
"dimensionFilter": {
"andGroup": {
"expressions": [
{
"filter": {
"fieldName": "googleAdsCustomerId",
"stringFilter": {
"matchType": "EXACT",
"value": "*********",
"caseSesnsitive": false
}
}
}
]
}
}
}
}
What I expect when querying the 'transactions' dimension is, as the API schema describes, "The count of transaction events with purchase revenue. Transaction events are in_app_purchase, ecommerce_purchase, purchase, app_store_subscription_renew, app_store_subscription_convert, and refund."
The response from my query comes back with these numbers, for an example campaign:
{
"dimensionValues": [
{
"value": "***********",
"oneValue": "value"
},
{
"value": "Example Campaign",
"oneValue": "value"
}
],
"metricValues": [
{
"value": "2482480",
"oneValue": "value"
},
{
"value": "6492393600000",
"oneValue": "value"
},
{
"value": "331",
"oneValue": "value"
},
{
"value": "31374.205645000002",
"oneValue": "value"
}
]
}
However, if, in the GA4 dashboard, I attempt to view a report of purchase conversions by campaign over the same date range, this is what is displayed for the 'example campaign':
6 Example Campaign 239.47 31,981.63
Where 239.47 is the number of transactions, and 31,981.63 is the event value (transaction revenue). Notably, the transactions are off by over 25%. The revenue/event value is similar but also off by a slight amount. This is consistent for all campaigns under the client, with the API response being significantly (but by varying percentages) higher. The dashboard value is always lower. These numbers don't change if the report is run on different dates.
Additionally, I suspected that there may be some additional event being tracked under the transactions api field that was not displaying in the dashboard, so I also tried adding this filter to my query:
{
"filter": {
"fieldName": "eventName",
"stringFilter": {
"matchType": "EXACT",
"value": "purchase",
"caseSesnsitive": false
}
}
}
the transactions field still came back as 331.
What I want to figure out is if I'm querying the wrong field, the frontend is under reporting data, or the API is over reporting data. I found that I was not able to post on the official GA4 Issue tracker so I've come here.
Someone better versed in GA4 at my company explained that the issue here had to do with dimension scopes. The transactions field is session scoped, but the dimensions I was pulling in were event scoped. This means that the numbers being output were basically meaningless. As a matter of fact through the dashboard, it won't let you create a free-form explore report using the combination of fields that I did in my query, explaining that the metrics are incompatible. It appears to just be a bug that the API would allow that to happen.
As an alternative, I'm now pulling the conversions field, with a filter on eventName as listed above, including only purchase events.

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 do you get data from google analytics -v4 for acquisitions - campaigns?

Can someone give an example of the query to be used?How do we access the acquisitions and then campaign data from that ?
The simplest method would be to make an Analytics Reporting API V4 request with the ga:newusers metric and the ga:source, ga:medium, ga:campaign.
POST https://analyticsreporting.googleapis.com/v4/reports:batchGet
{
"reportRequests":
[
{
"viewId": "1174",
"dateRanges":
[
{
"startDate": "2014-11-01",
"endDate": "2014-11-30"
}
],
"metrics":
[
{
"expression": "ga:newusers"
}
],
"dimensions":
[
{
"name": "ga:campaign"
},
{
"name": "ga:source"
},
{
"name": "ga:medium"
}
]
}
]
}
And again in the API Explorer.
The API also allows you to construct a cohort request to measure engagement overtime.
If you are new to Google's APIs, they make available many client libraries as well as set of quickstart guides.

Cohort Analysis in Google Analytics

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.

Resources