Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build 'real time visitors on site', Google API?

I'm building a custom admin dashboard for users on our site who create posts. I want to show them the active amount of visitors on their posts only (not on the entire site).

I want it to act exactly like GA does it:

enter image description here

I was originally thinking of building this from scratch, but in retrospect it might be easier to use the GA API?

I've stared at the docs for forever and I'm just not groking it, so I'm coming here for help.

We have ~5,000 posts total, and I some people on our site have authored over 1000 posts, so the 'input' to GA will be anywhere from 1 to 1000+ slugs (for only their posts).

I want a combined amount of on-site traffic for their posts only.

Optionally, maybe it would have to be reversed... I'm not sure if GA can show it, but even better probably would be to get a content breakdown of the realtime visitors from the API, with 5000 max results. From there I can filter through the result set slugs (along with then number of users on each), and compare those results to each slug which belongs to that user, then just sum the totals on my end.

Is this something the Google API could help me with? which API endpoint would I need to use? Is it possible to have 5000+ max results for URLs with traffic on them from the API?

Thanks!

like image 633
Tallboy Avatar asked Oct 20 '22 05:10

Tallboy


1 Answers

Yes, it is possible.

It seems that you should utilize Real Time Data: get endpoint.

Additionally, to limit results for specific pages (posts) only, you should use dimension filters (filters which will select only specific page views before calculating aggregated result), and 'ga:pagePath' looks like the one you need:

ga:pagePath

UI Name: Page A page on your website specified by path and/or query parameters. Use in conjunction with hostname to get the full URL of the page.

Source

You might prefer using ga:pageTitle instead, if you have similar title for posts of a single author, and you haven't got common path elements in posts of the same author.

So you do something like:

GET https://www.googleapis.com/analytics/v3/data/realtime
ids=ga:<your_analytics_id>
metrics=rt:activeUsers
dimensions=rt:pagePath
filters=rt:pagePath=~/authors/123/*

Please notice that there maybe slight difference in real time and non-realtime API (e.g. use of 'rt' instead of 'ga' above), and generally realtime-API is still in beta.

Generally speaking, you should go here: Real Time Reporting API - Developer Guide and look through the links in the table of contents (left part of the page).

What about 'building from scratch' idea: it's rather simple from the developer's perspective, but it could be complex from the dev-ops perspective. I.e., it's not a problem to write code which would aggregate such metrics. But it could be a problem to make a system which will sustain required for that task amount of requests per second.

like image 109
EugZol Avatar answered Oct 22 '22 02:10

EugZol