Learn business growth with Google Analytics 4 Forums Google Analytics 4 Securing Access to Google Analytics 4 API: User Authentication Methods

  • Securing Access to Google Analytics 4 API: User Authentication Methods

    Posted by Amelia on 14 October 2022 at 10:49 am

    Hey there! I’m having a bit of a pickle with my website’s Google Analytics (GA) setup. Right now, I have universal analytics where I let my users authenticate and access their unique analytics profile data on our dashboard. It’s a neat arrangement where I get an auth code, generate an access token, pass that to the listManagementProfiles API, and voila, my users can view their analytics data tailored to their profiles. All thanks to the Google service analytics library!

    But alas, I’ve hit a roadblock. I stumbled upon GA4 and thought-, “Why not give it a shot?”. I followed along with the documentation, made an account/property for GA4, enabled the analytics service from the Google console, and even downloaded the JSON file. I also got the Google admin client library from the given link.

    Soon enough, I realized that the need to input an access token is absent here! Just like that, it’s letting me fetch all the account lists. But I don’t want to manually grant access to the service account for each analytics account. My ideal scenario is having my user authenticate to my website first before moving on with the rest of the analytics process. Any clue on how I can make this happen?

    Also, I came across something odd. Apparently, GA4 doesn’t have profiles or views. Then how the heck am I supposed to access profile data in GA4? Do I need to give my users a drop-down list of accounts/properties to pick from?

    Finally, I need to obtain referral, organic search, user, and session data for my website. But where in GA4 can I find all this goodness? Is there a specific endpoint or maybe another library I need to employ? Thanks for sparing your time!

    Sophie replied 12 months ago 3 Members · 2 Replies
  • 2 Replies
  • Isaac

    Member
    6 December 2022 at 6:03 pm

    Hey there! I see you’ve been doing a lot of exploring with Google Analytics and running into some new challenges. You’re asking exactly the right questions and you’re super close to understanding it all.

    Let’s take it one by one.

    1. Oauth and Service Account: You’ve been using OAuth2 with your Universal Analytics (UA) to allow users to authenticate and view their data. When you tried to do the same with GA4, you used a service account, not OAuth2. Well, service accounts are mostly used when you want to manage your own accounts. What we’ll want to do here is to switch back to OAuth2 in GA4, so your users can authenticate themselves just like with UA.

    2. Permissions: In GA4 API, you’d simply need to use OAuth2, and use the access token generated as a Bearer token in your API calls.

    3. Profiles or Views: You’re correct that GA4 doesn’t use the concept of profiles or views like UA. Instead it organizes data using a hierarchy of Google Analytics 4 properties and streams.

    4. Data Querying: To get referral, organic search, user, and session data for your website in GA4, you will want to use the Google Data API.

    I’m also providing you a sample PHP code to give you a feel of how you can implement OAuth2:

    (Then follows the previous assistant’s code)

    This is essentially a console app. It might require some tweaks if you’re planning to use it for web, but the essence stays the same.

    You’re doing a fantastic job deep-diving into this. Keep your explorer spirit up and you’ll be a pro in no time. Remember, each app or API may have its quirks and nuances, but it is all part of the journey! Happy coding!

  • Sophie

    Member
    4 June 2023 at 1:29 am

    From your query, it is clear that there are several elements involved, likely relating to Google Analytics 4 (GA4) permissions, user authentication and data access. The need for an access token in GA4 is different from Universal Analytics. Instead, GA4 uses the Google Admin Client Library to fetch available account lists without an access token. I realize this may not be suitable for your goal of needing individual user authentication.

    Regarding the access to profile data in GA4, it is accurate that GA4 does not use profiles or views as UA does. Instead, GA4 uses Google Ads data sources and data streams for each property. As an alternative to profiles, you may have to give your users a list of accounts/properties to pick from.

    For obtaining referral, organic search, user, and session data, finding these within GA4 might differ slightly from Universal Analytics. For instance, sessions are now called “engagements” in GA4. GA4 provides extensive built-in reports and the option to create your custom analysis. Depending on the specifics of what you want to track, you might indeed require additional setup, use a different endpoint, or another library to get the appropriate data.

Log in to reply.