简体   繁体   中英

Google Analytics Reporting API: number of active users in Javascript

on my site I want to include the number of active users (real-time visitors) somewhere on a banner or whatsoever. The only problem is, I can only do it via Javascript (no PHP; Java, Phyton...), but I don't want users to sign up to see it. I spent hours checking the Google Analytics Reporting API and I understand that what I want to achieve is probably not possible, but wanted to check whether some of you has found workarounds that could work for me, too.

Thanks much to everyone. A.

Here is the documentation of Google Analytics API for realtime: https://developers.google.com/analytics/devguides/reporting/realtime/v3/reference/data/realtime/get#try-it

But if you want a simple counter you already have plenty of sites that offer that, or you can implement one for your self.

This one looks quite simple: https://github.com/felladrin/online-visitors-counter

You cannot use the GA API without a form of authentication , therefore connecting GA directly to your website without client login isn't possible.

As a workaround , you can use a middleware solution to pull data from the GA API, and then pull data from that midddleware to your website. Here is an example:

GA API ---> Google Sheets ---> Website
  1. Use the GA add-on to pull your data to a Google Sheet
  2. Import that data into another Google Sheet with IMPORTRANGE
  3. Publish the Google Sheet from 2 so it's publicly readable via API (for instance as CSV). You will be provided with a URL that looks like https://docs.google.com/spreadsheets/d/e/{some-uid}/pub?gid=0&single=true&output=csv
  4. Fetch data from that URL from the website (eg with jQuery )

Note: you probably don't have to do step 2 if you're careful, and can publish as CSV your sheet from steo 1 directly: since it's a CSV export, people cannot get to the original spreadsheet so no security risk. But just to be on the safe side (eg you could publish the whole spreadsheet by mistake), I would do step 2.

As for which middleware you want to use, there's probably an infinite number of options :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM