Reporting API v4 Google analytics - Ecommerce Missing Transactions - google-analytics

I'm currently having an issue when comparing my custom table with Google Analytics Ecommerce UI view where a significant amount of transaction data are missing in the table.
Below is the portion of the code snippet I used to request the transaction report.
"dimensions": [
{"name": "ga:dateHour"},
{"name": "ga:channelGrouping"},
{"name": "ga:source"},
{"name": "ga:medium"},
{"name": "ga:campaign"},
{"name": "ga:transactionId"}
],
"metrics": [
{"expression": "ga:sessions"},
{"expression": "ga:transactionRevenue"},
{"expression": "ga:transactionTax"},
{"expression": "ga:transactionShipping"}
]
`

Related

How to use "ga:isoWeek" in GA4

I need to have this year data by week from GA4.
I used the below API:
{
"dateRanges": [{ "startDate": "2021-1-1", "endDate": "yesterday" }],
"dimensions": [
{
"name": "week"
}
],
"metrics": [{ "name": "activeUsers" }]
}
But, I’d like to have the week from Monday to Sunday as ISO week, instead Sunday to Saturday.
I have known UA had "ga:isoWeek" as dimensions, but how about GA4?
Ga4 does not support isoWeek at this time. You should check out the API Dimensions & Metrics for the Google analytics data api it shows all the dimensions and metrics that work with GA4.

Is it possible to manipulate the dimension name within the Google Analytics API response in the query itself

Here is a sample query that I created to fetch Google Analytics data:
response = service.reports().batchGet(
body={
'reportRequests': [
{
'viewId': 'xxxx',
'dateRanges': [{'startDate': '2021-01-14', 'endDate': '2021-01-15'}],
'metrics': [
{'expression': 'ga:pageViews'},
{'expression': 'ga:sessions'},
{'expression': 'ga:itemRevenue'},
{'expression': 'ga:hits'},
{'expression': 'ga:sessionDuration'},
],
# Get Pages
'dimensions': [
{"name": "ga:clientId"},
{"name": "ga:pagePath"},
{"name": "ga:dateHourMinute"},
{"name": "ga:shoppingStage"},
{"name": "ga:source"},
{"name": "ga:campaign"},
],
# Filter by condition
"filtersExpression": "ga:clientId==yyyy.zzzz",
'orderBys': [{"fieldName": "ga:dateHourMinute", "sortOrder": "DESCENDING"}],
'pageSize': 500
}]
}
).execute()
Sample response:
{'dimensions': ['yyyy.zzzz',
'/products/pants-green?variant=456456456',
'202101142347',
'ALL_VISITS',
'newsletter',
'2021_01-pre-sale',
'282'],
'metrics': [{'values': ['0',
'0',
'0.0',
'1',
'0.0']}]},
Is it possible to define alternate naming for the dimensions in the response within the query itself, e.g.
strip the variant part from the page path with regex,
change the wording for "ga:shoppingStage" from ALL_VISITS to something else?
Or is this something which needs to be done in post-processing?
The dimensions and metrics are standard within Google analytics. The response returned to you from the API is simply the name of the dimensions and metrics from the API.
Even if you have your own custom dimensiosn and metrics set up the API is still just going to return it with the name ga:dimensionXX
If you want to change the names your going to have to do that locally after the data is returned to you.

Why Google Analytics "minute" dimension doesn't work?

I try to execute the following request usign Google Reporting API:
"reportRequests": [
{
"metrics": [
{
"expression": "ga:sessions"
}
],
"dimensions": [
{
"name": "ga:date"
},
{
"name": "ga:minute"
}
],
"dateRanges": [
{
"startDate": "2018-01-02",
"endDate": "2018-04-17"
}
],
"viewId": "validViewId",
"hideValueRanges": true,
"hideTotals": true
}
]
Unfortunately I get no data. But when I use only "date" dimension, without "minute", I get some data in response.
Could someone please explain such a weird behavior?
As you are querying historical data (here > 2 years) the level of granularity of your data would be impacted by the retention period defined on property level. Meaning that beyond the retention period you can only query a combination of dimensions that are covered by Google Analytics standard aggregated tables (GA Help Center)
In your case the combination "Session | Date | Minute" is not part of Google Analytics aggregated tables, however "Session | Date" is.
You can recreate this phenomenon by using your query on different time ranges: within the retention period vs. outside the retention period.

Google Analytics API Revenue Discrepances

I have discrepancies in the revenue metric, between the data I collect from the Google Analytics API and the custom reports in the user interface.
The discrepancies for each value maintain the same rate, where the data collected through the API is greater than the data in the custom reports.
This is the body of the request I'm using:
{
"reportRequests":[
{
"viewId":"xxxxxxxxxx",
"dateRanges": [{"startDate":"2017-07-01","endDate":"2018-12-31"}],
"metrics": [
{"expression": "ga:transactionRevenue","alias": "transactionRevenue","formattingType": "CURRENCY"},
{"expression": "ga:itemRevenue","alias": "itemRevenue","formattingType": "CURRENCY"},
{"expression": "ga:productRevenuePerPurchase","alias": "productRevenuePerPurchase","formattingType": "CURRENCY"}
],
"dimensions": [
{"name": "ga:channelGrouping"},
{"name": "ga:sourceMedium"},
{"name": "ga:dateHour"},
{"name": "ga:transactionId"},
{"name": "ga:keyWord"}
],
"pageSize": "10000"
}]}
This is an extract of the response:
{{
"reports": [
{
"columnHeader": {
"dimensions": [
"ga:channelGrouping",
"ga:sourceMedium",
"ga:dateHour",
"ga:transactionId",
"ga:keyWord"
],
"metricHeader": {
"metricHeaderEntries": [
{
"name": "transactionRevenue",
"type": "CURRENCY"
},
{
"name": "itemRevenue",
"type": "CURRENCY"
},
{
"name": "productRevenuePerPurchase",
"type": "CURRENCY"
}
]
}
},
"data": {
"rows": [
{
"dimensions": [
"(Other)",
"bing / (not set)",
"2018052216",
"834042319461-01",
"(not set)"
],
"metrics": [
{
"values": [
"367.675436",
"316.55053699999996",
"316.55053699999996"
]
}
]
},
...
So, if I create a custom report in the Google Analytics user interface and look for the transaction ID 834042319461-01, I get the following result:
google Analytics custom report filtered by transaction id 834042319461-01
In the end I have a revenue value of 367.675436 in the API response, but a value of 333.12 in the custom report, its a 10.37% more in the value of the API. I get this 10.37% increase for all values.
¿Why I'm having these discrepance?
¿What would you recomend to do in order to solve these problem?
Thanks.
My bet is that you're experiencing sampling (is your time range in the UI lower than in the API?): https://support.google.com/analytics/answer/2637192?hl=en
Sampling applies when:
you customize the reports
the number of sessions for the overall time range of the report (whether or not your query returns less sessions) exceeds 500K (GA) or 100M (GA 360)
The consequence is that:
the report will be based on a subset of the data (the % depends on the total number of sessions)
therefore your report data won't be as accurate as usual
What you can do to reduce sampling:
increase sample size (will only decrease sampling to a certain extend, but in most cases won't completely remove sampling). In UI it's done via the option at the top of the report, in the API it's done using the samplingLevel option
reduce time range
create filtered views so your reports contain the data you need without needed to customize reports
Because you are looking at a particular transaction ID, this might not be a sampling issue.
If the ratio is consistent, from your question it seems to be 10.37%. I believe this is the case of currency that you are using.
Try using local currency metric API calls when making monetary based calls.
For example -
ga:localTransactionRevenue instead of ga:transactionRevenue

GA: How to access Cohort Analysis via Analytics API?

Cohort dimensions and metrics are listed here. Yet, when I try to query it using API (e.g. using Query Explorer) an error 400 occurs.
One of queries I've tried is: metrics = ga:cohortActiveUsers and dimensions = ga:cohortNthDay .
Is is possible to query Cohort Analysis report via API?
The problem you are having is because the Query explorer uses the v3 of the Google Analytics API. If you look at the Dimensions and Metrics Explorer you will notice that these dimensions were added in the Analytics Reporting API V4.
The error message you are getting is incorrect and should be corrected soon. It should state something more like This metric cannot be used in Version 3 of the API. You caught this while we where in the process of rolling out the new API. Which has now been officially released see change log
To make use of these new dimensions and metrics you must construct a V4 cohort request:
POST https://analyticsreporting.googleapis.com/v4/reports:batchGet
{
"reportRequests": [{
"viewId": "XXXX",
# No date range is required in the request
"dimensions": [{"name": "ga:cohort" },{"name": "ga:cohortNthDay" }],
"metrics": [
{"expression": "ga:cohortActiveUsers" },
{"expression": "ga:cohortTotalUsers"}
],
"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"}
}]
}
}]
}
It is possible to compose a cohort requests by using the Request Composer tool, in the Cohort Request tab.
As you select the options in the Set query parameters section, the request payload is shown below.
Hope it helps.

Resources