Vitor EL
Vitor EL

Reputation: 555

Is there a way to retrieve Google Analytics 4 data on a schedule using Node.js?

This is what I want to achieve:

  1. Ask the user to authorize the collection of their data on a Google Analytics 4 property (or Universal Analytics but I would rather not)
  2. Programmatically retrieve and store the data every n-hours

I was able to do (1) client-side by asking for authorization with google's OAUTH2 and making a call to Reporting API v4 https://developers.google.com/analytics/devguides/reporting/core/v4 using gapi on the front-end.

However, I'm not sure how to do it on a schedule without user interaction. I've searched Google's API docs and I believe there's a way to do it in python https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-py but I am currently limited to Node and the browser. I guess I could make a server in python that does the data fetching and connects with the Node application, but that's yet another layer of complications that I'm trying to avoid. Is there a way to do everything in Node?

Upvotes: 2

Views: 335

Answers (1)

Priyashree Bhadra
Priyashree Bhadra

Reputation: 3597

GCP APIs are all documented in a way which allows everyone to generate client libraries in a variety of languages, including node.js. The documentation for the node.js client for Analytics Reporting is here.

For the question of how to schedule this on GCP, I would recommend you to use Cloud Scheduler. This will hit an endpoint running on Cloud Run, which will do the actual work. Alternatively, if you already have a service running somewhere else, you can simply add the required endpoints there and point Cloud Scheduler to it.

The overall design which I would suggest you goes something like this:

  • Build a site which takes the user through the OAUTH2 login process, requesting the relevant Google Analytics Reporting API scopes required to make the request.
  • Store the obtained credentials in their user database.(preferably Firestore in Datastore mode)
  • Set up a Cloud Run service (or anything else), with two endpoints
  • Iteration endpoint: Iterate through the list of users and add tasks to Cloud Tasks to hit the download endpoint for each one.
  • Download endpoint: Takes a user ID (e.g. as a query parameter) and performs the download for this user. You will need to load the credentials for the user from the database and use this to access the reporting API.
  • Store the downloaded data in the desired location, e.g. Cloud Storage, Firestore, Cloud SQL, etc.
  • Set up Cloud Scheduler to hit the iteration endpoint at the desired frequency.

For the GCP services mentioned above, basically everything other than Analytics, you may use the "cloud" clients for node.js, which are available here

Note : The question you have asked is a very broad question and this answer is just a suggestion. You may think about other designs whichever works best for you.

Upvotes: 2

Related Questions