javascriptgoogle-analytics-apigoogle-reporting-api

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.


Solution

  • 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 :)