0

I am making a website like flippa to sell established websites, I want to verify website traffic like flippa do, I searched for how to get reports of visits last 12 months but I can't, what I do make client click on link

https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=814867900881-28dlacj0v68nh9suspfjlnvjgscalaql.apps.googleusercontent.com&prompt=consent&redirect_uri=http://localhost:3000/redirect&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fanalytics.readonly&state=9612

to get code then use that code to get access_token

POST https://accounts.google.com/o/oauth2/token 
code=4/gXCN77EWLDCO_fake_p2tvfakezOg6Mn0fakej2vA.giyP3fakejxeAeYFZr95uygvU3j0dumQI&
client_id=104608secret-secret-secret-secret.apps.googleusercontent.com&
client_secret=90V0FAKE_WkFAKExrHCZti&
redirect_uri=http://www.mywebapp.com/oauth2callback&
grant_type=authorization_code

then I get access token

{
  "access_token": "ya29.Il-yBwla5jnTECDlX5rbVk_Oq4hireOUmSzeaTWW2BgYYsBCoPn6pKnQVxIUQtGc0BXblEq_2ZQ-zhrrGeL9xYYuSyd8lvRAoPBUVKUN8liBwx-w8ok2oGh2Cknql2ucew",
  "expires_in": 3600,
  "refresh_token": "1//031hXU_jNeKEECgYIARAAGAMSNwF-L9Irds9iUaPkcGDDOSHP2-8-1FKYBRW1GaBz1fLzEoDnEcNIm89k-bZzYWXAqie5Or-Fg94",
  "scope": "https://www.googleapis.com/auth/analytics.readonly",
  "token_type": "Bearer"
}

and don't know what I do next and if what I do is the right way I need the correct way to get user GA reporting

2 Answers2

0

What you have done so far covers your authentication flow. Now you will need to make a call to the Analytics Reporting API and supply the access token you have created. Here is a reference for setting up your request: https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet There are also samples available for how to request data.

jasonandmonte
  • 1,869
  • 2
  • 15
  • 24
0

Depending on the system you have, this may help. https://developers.google.com/analytics/devguides/reporting/core/v4

In essence, you add the access token in the google client then perform the request.

For node.js, I found this site that does the particular in node.js although this uses JWT. https://flaviocopes.com/google-analytics-api-nodejs/

const { google } = require('googleapis')
const scopes = 'https://www.googleapis.com/auth/analytics.readonly'
const jwt = new google.auth.JWT(process.env.CLIENT_EMAIL, null, process.env.PRIVATE_KEY, scopes)

const view_id = 'XXXXX'

async function getData() {
  const response = await jwt.authorize()
  const result = await google.analytics('v3').data.ga.get({
    'auth': jwt,
    'ids': 'ga:' + view_id,
    'start-date': '30daysAgo',
    'end-date': 'today',
    'metrics': 'ga:pageviews'
  })

  console.dir(result)
}

getData()

Also, google APIs for Node.js for reference: https://www.npmjs.com/package/googleapis

It contains how to set the access token to set to the oauth2Client and use these in the APIs.

Hope this helps :)

Don F.
  • 123
  • 8
  • I get "message": "User does not have sufficient permissions for this profile.", – Abanoub Istfanous Nov 23 '19 at 04:38
  • Are you able to allow it in your analytics settings in the console. I've seen some similar problems and their solution is to use VIEW id. https://stackoverflow.com/questions/14525565/not-sufficient-permissions-google-analytics-api-service-account – Don F. Nov 23 '19 at 07:10