Learn business growth with Google Analytics 4 Forums Google Analytics 4 How can I effectively utilize the Google Analytics API – Version 4?

  • How can I effectively utilize the Google Analytics API – Version 4?

    Posted by Xavier on 16 August 2022 at 11:08 pm

    Hey folks, so here’s what I did.

    1. I logged a user in using Google account and scored a token. The code looks like this:

    ^ LaravelSocialiteTwoUser {#1512 ▼
    +token: “ya29 — 51 ◀”
    +refreshToken: null
    +expiresIn: 3599
    Sweet, that part worked.

    2. I got stuck trying to pull the list of account summaries the user can access, to find the viewId. Not sure where to find the key for this line: ‘https://analytics.googleapis.com/analytics/v3/management/accountSummaries?key=**WHERE_GET_THIS_KEY?**’

    The code here goes like this:

    –header ‘Authorization: Bearer ya29 — 51’
    –header ‘Accept: application/json’
    Can anyone help me figure out where to dig up this key?

    3. Now that I’ve hopefully accomplished those things, I want to query with:

    curl -X POST -H “Content-Type: application/json” -H “Authorization: Bearer ya29 — 51” -d ‘{
    “viewId”: “**FROM_PREVIOUS_QUERY**”,
    “dateRanges”: [{“startDate”: “2014-11-01”, “endDate”: “2014-11-30”}],
    “metrics”: [{“expression”: “ga:users”}]
    }’ https://analyticsreporting.googleapis.com/v4/reports:batchGet
    Couple of questions here folks

    – Can anybody help me out with getting the key for the second query?
    – Is the third query in the right ballpark?

    Thanks, guys.

    By the way, I used these resources:
    – [BatchGet Guide](https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet?authuser=1)
    – [Google Analytics Basics](https://developers.google.com/analytics/devguides/reporting/core/v4/basics)
    – [Account Summaries List](https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtReference/management/accountSummaries/list?hl=ru&apix=true#try-it)

    Rajesh replied 11 months ago 3 Members · 2 Replies
  • 2 Replies
  • Ava

    11 October 2022 at 8:30 am

    You don’t actually need a key to use that second query. It’s a common misconception but Google APIs don’t always need a key – just the access token you obtained when the user logged in. As for your third query, it looks spot on—assuming the viewId from the second query is correctly placed into the “**FROM_PREVIOUS_QUERY**” spot. Good luck with your code.

  • Rajesh

    16 March 2023 at 8:07 pm

    The person is explaining what they did to use an API from Google Analytics. Firstly, they successfully logged into a user account using Google and generated a token. Secondly, they tried to retrieve a list of account summaries that the user can access, but got stuck because they couldn’t find the required key. They provided the code they used and asked if anyone knows where they can find this key. Lastly, they want to carry out a query using a command, providing the snippet of the command they used, and asked if it is correct or not. They also asked if anyone can help find the key for the second query. They concluded by stating they used several resources, providing links to these resources.

Log in to reply.