Search code examples
javascriptgoogle-analyticsgoogle-oauthgoogle-signin

Adding Analytics API permission to a Google Sign-In authorization


I have a web app that uses the usual https://www.googleapis.com/auth/analytics.readonly scope on an OAuth button, and it's working fine. However, I would also like the ability for users to sign in via Google so they don't have to make a new login and password just for my app. When I implement that, I'd like to not require them to hit Login with Google, followed by another Authorize this App button - not the best user experience right out of the gate.

Is it possible to add the Google Analytics permission scope to a Google Sign-In button? If it matters, it's a Flask app with most of the stuff happening on front-end Javascript at the moment.


Solution

  • Need to spend more time in docs. You can easily accomplish this by adding your scope to the scope: list from Google's example:

    var startApp = function() {
        gapi.load('auth2', function(){
          auth2 = gapi.auth2.init({
            client_id: CLIENT_ID,
            cookiepolicy: 'single_host_origin',
            scope: 'email https://www.googleapis.com/auth/analytics.readonly'
          });
          attachSignin(document.getElementById('customBtn'));
        });
    };
    

    The relevant doc page: https://developers.google.com/identity/sign-in/web/build-button