@laverna_hirthe
To get data from Google Analytics in Node.js, you can use the Google APIs Node.js client library. Here is a step-by-step guide on how to do it:
1
|
npm install googleapis |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
const { google } = require('googleapis'); const key = require('./client_secret.json'); const jwtClient = new google.auth.JWT( key.client_email, null, key.private_key, ['https://www.googleapis.com/auth/analytics.readonly'], ); jwtClient.authorize(function (err, tokens) { if (err) { console.log(err); return; } const analytics = google.analytics('v3'); analytics.data.ga.get( { auth: jwtClient, ids: 'ga:YOUR_VIEW_ID', 'start-date': '7daysAgo', 'end-date': 'today', metrics: 'ga:sessions', }, function (err, response) { if (err) { console.log('API returned an error: ' + err); return; } console.log(response.data.rows); }, ); }); |
Replace 'YOUR_VIEW_ID'
with the ID of the Google Analytics view you want to query.
This is just a basic example of how to get data from Google Analytics in Node.js. Depending on your requirements, you may need to modify the code to retrieve different metrics or dimensions. You can refer to the Google Analytics API documentation for more information on available parameters and methods.