Ad

Google Analytics Steps To Get Number Of Views Per Page From An API

I want to start somewhere. This is my use case.

  1. I have a restAPI written in node.jsexpress framework.
  2. I have a mobile application written in the react native.
  3. Also I have a web application for the administration purpose of the mobile app, written in Vue.js

Mobile app is food app. Where you can search restaurants or dishes of a restaurant.

I want to pass the restaurant id and get the restaurant views of the app with in the last 7 days. Pretty straight forward requirement. But hard to implement. Since I don't know where to start from.

I went through the docs and have found there are 7 APIs.

  1. Core reporting API
  2. Management API
  3. Embed API
  4. User deletion API
  5. Multi channel reporting API
  6. Real time API
  7. Meta data API

End of the day, I would love to do something similar to this with the API.

showMeTheView(restaurant_id)

Also If I want to pass additional parameter to say get only last month views count.

showMeTheViews(restaurant_id, last_month)

I can't figure out what are the essential steps to achieve my requirement?

What need to be done in the react-native app? What need to be done in the vue.js web app? What need to be done in between these two?

Ad

Answer

First off you should be using the Core reporting api this is the api that can be used to extract data from Google analytics. The Json object used to extract data form Google analytics is quite extensive batch get

It will make your life a lot easier if you use the Google apis node.js client libray code ripped from sample found here

'use strict';

const {google} = require('googleapis');
const sampleClient = require('../sampleclient');

const analyticsreporting = google.analyticsreporting({
  version: 'v4',
  auth: sampleClient.oAuth2Client,
});

async function runSample() {
  const res = await analyticsreporting.reports.batchGet({
    requestBody: {
      reportRequests: [
        {
          viewId: '65704806',
          dateRanges: [
            {
              startDate: '2018-03-17',
              endDate: '2018-03-24',
            },
            {
              startDate: '14daysAgo',
              endDate: '7daysAgo',
            },
          ],
          metrics: [
            {
              expression: 'ga:users',
            },
          ],
        },
      ],
    },
  });
  console.log(res.data);
  return res.data;
}

// if invoked directly (not tests), authenticate and run the samples
if (module === require.main) {
  const scopes = ['https://www.googleapis.com/auth/analytics'];
  sampleClient
    .authenticate(scopes)
    .then(runSample)
    .catch(console.error);
}

// export functions for testing purposes
module.exports = {
  runSample,
  client: sampleClient.oAuth2Client,
};
Ad
source: stackoverflow.com
Ad