Reputation: 45
I am struggling with getting the currently active users from Reporting API v4 via JavaScript. I found this part of documentation which is dealing with it, where is it available as "rt:activeUsers", but it is for v3, not v4 - https://developers.google.com/analytics/devguides/reporting/realtime/dimsmets/. It seems like there is no such option available in documentation of v4 - https://developers.google.com/analytics/devguides/reporting/core/dimsmets#cats=user
Did I just overlook something? See my code below
<!-- API CALL -->
<script>
// VIEW ID
var view_id = '0000000';
var start_date = '7daysAgo';
var end_date = 'today';
// LOAD DATA FROM GOOGLE ANALYTICS
function loadAnalytics() {
gapi.client.request({
// CONFIGURATION
path: '/v4/reports:batchGet',
root: 'https://analyticsreporting.googleapis.com/',
method: 'POST',
// REQUEST
body: {
reportRequests: [{
// VIEW ID
viewId: view_id,
// DATA RANGE FOR RESULTS
dateRanges: [{
startDate: start_date,
endDate: end_date
}],
dimensions: [
{ name:"ga:browser" },
{ name:"ga:browserVersion" },
{ name: "ga:userType"}
],
// REQUESTED DATA
metrics: [
{ expression: 'ga:hits' },
{ expression: 'ga:users' },
{ expression: 'ga:newUsers' },
{ expression: 'ga:sessions' },
{ expression: 'ga:avgSessionDuration' },
{ expression: 'ga:percentNewSessions' },
{ expression: 'ga:sessionsPerUser'},
{ expression: 'ga:bounces' },
{ expression: 'ga:bounceRate' },
{ expression: 'ga:activeUsers'}
]
}]
}
// PUSH OBTAINED DATA TO DISPLAYING FUNCTION
}).then(prepareVariables, console.error.bind(console));
}
Upvotes: 2
Views: 4582
Reputation: 116918
Reporting API V4
The most advanced method to programmatically access report data in Google Analytics. Build pivot tables as well as cohort, lifetime value, and advanced segmentation reports with the most flexible access to your data.
The Google Analytics reporting v4 api can be used to request data from Google analytics. This is not real time data. This id data that appears in the google analytics website as soon as it has been processed. It can take time for data to appear in this API any where from a few hours (This data is probably not actuate) to a few days.
This is due to data latency doc
Processing latency is 24-48 hours. Standard accounts that send more than 200,000 sessions per day to Analytics will result in the reports being refreshed only once a day. This can delay updates to reports and metrics for up to two days. To restore intra-day processing, reduce the number of sessions your account sends to < 200,000 per day.
Get user activity occurring on a property right now. Realtime reports are updated within seconds so you can build live dashboards to monitor how users are interacting with your property at any moment.
The realtime api is very basic. not all of the data is there. but its this api that you should be using
Google Analytics APIs
This is in response to the comment below. The google Analytics APIs are split into several APIs.
So just because the Reporting api, management api, and realtime apis are all have v3 versions does not mean that they are the same. The reporting api does NOT have realtime data that's not what its for that's what the realtime api is for.
Upvotes: 4