Google

General

Google Analytics

Google API



  • console API explorer reference
    authorization (credentials)

    API key
    OAuth 2.0
    web
    installed service
    client
    server

    Python Python
    JavaScript
    PHP
    Python
    Java
    Python
    Java
    Python
    PHP

    Google API Client Libraries
    API Manager


    Library PyDoc

    Using API Keys


    Using OAuth 2.0 for Web Server Applications

    Using OAuth 2.0 for Installed Applications

    Using OAuth 2.0 for Server to Server Applications

    services
    Analytics
    Analytics
    v3
    APIs

    Management API - Authorization (scopes)
    -




    Hello Analytics API: Python quickstart for installed applications

    Hello Analytics API: Python quickstart for service accounts

    ...














  • Google API console
    • Credentials


      • access to
        private user data


        download format
        provided fields
        API key
        Simple API access -



        • API key
        OAuth 2.0

        Authorized API access x
        • scope
        • access token
        • refresh token
        • client id
        • client secret
        Web application (three-legged) client IDs
        json
        • web
          • client_id
          • client_secret
          • auth_uri: https://accounts.google.com/o/oauth2/auth
          • token_uri: https://accounts.google.com/o/oauth2/token
          • project_id
          • auth_provider_x509_cert_url
        Installed application (three-legged) (mobile and desktop) client IDs
        json
        • installed
          • client_id
          • client_secret
          • redirect_uris
          • auth_uri
          • token_uri
          • project_id
          • auth_provider_x509_cert_url
        Service account (server to server)
        (two-legged OAuth; 2LO) client IDs
        json
        • type: service_account
        • client_id
        • auth_uri
        • token_uri
        • project_id
        • private_key_id
        • private_key
        • client_email
        • auth_provider_x509_cert_url
        • client_x509_cert_url
        PKCS #12
  • Python
  • curl
  • bash
    • #!/bin/bash

      EXPECTED_ARGS=1
      if [ $# -ne $EXPECTED_ARGS ]
      then
          cat <<EOF
      Usage: `basename $0` service_credentials_path

      Get analytics information using OAuth service authentication.

      Examples:
        `basename $0` service-account.json
      EOF
          exit 1
      fi

      credentials_path=$1

      # private key from json file
      private_key=$(cat $credentials_path | jq -r '.private_key')

      # jwt header
      header=$(jq -c -n '{alg:"RS256",typ:"JWT"}')
      header_b64=$(echo -n $header | base64 | tr -d '\n' | tr -d '=' | tr '/+' '_-')

      # jwt claim set
      iss=$(cat $credentials_path | jq '.client_email')
      scope=\"https://www.googleapis.com/auth/analytics.readonly\"
      aud=\"https://www.googleapis.com/oauth2/v4/token\"
      iat=$(date +%s)
      validity_seconds=3600
      exp=$(( iat + validity_seconds ))

      claim_set=$(jq -c -n "{iss: $iss, scope: $scope, aud: $aud, exp: $exp, iat: $iat}")
      claim_set_b64=$(echo -n $claim_set | base64 | tr -d '\n' | tr -d '=' | tr '/+' '_-')

      # jwt signature SHA256withRSA

      header_claim_set_b64=${header_b64}.${claim_set_b64}
      signature=$(echo -n "$header_claim_set_b64" | openssl dgst -sha256 -sign <(
      echo "$private_key" | sed 's/\\n/\n/g') | base64 | tr -d '\n' | tr -d '=' | tr '/+' '_-')

      # jwt
      jwt=${header_claim_set_b64}.${signature}

      # get token
      token_response=$(curl -s -H "Content-type: application/x-www-form-urlencoded" -X POST -d \
      "grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion=${jwt}" https://www.googleapis.com/oauth2/v4/token)

      token_type=$(echo "$token_response" | jq -r '.token_type')
      access_token=$(echo "$token_response" | jq -r '.access_token')

      # call analytics api with token

      # accounts list
      accounts_response=$(curl -s -H "Authorization: $token_type $access_token" https://www.googleapis.com/analytics/v3/management/accounts)
      echo "$accounts_response" | jq '.'
      first_account=$(echo "$accounts_response" | jq '.items[0]')
      first_account_id=$(echo "$first_account" | jq -r '.id')
      first_account_name=$(echo "$first_account" | jq -r '.name')
      echo "$first_account_id: $first_account_name"

      # properties list
      properties_response=$(curl -s -H "Authorization: $token_type $access_token" https://www.googleapis.com/analytics/v3/management/accounts/${first_account_id}/webproperties)
      echo "$properties_response" | jq '.'
      first_property=$(echo "$properties_response" | jq '.items[0]')
      first_property_id=$(echo "$first_property" | jq -r '.id')
      first_property_name=$(echo "$first_property" | jq -r '.name')
      echo "$first_property_id: $first_property_name"

      # view / profiles list
      profiles_response=$(curl -s -H "Authorization: $token_type $access_token" https://www.googleapis.com/analytics/v3/management/accounts/${first_account_id}/webproperties/${first_property_id}/profiles)
      echo "$profiles_response" | jq '.'
      first_profile=$(echo "$profiles_response" | jq '.items[0]')
      first_profile_id=$(echo "$first_profile" | jq -r '.id')
      first_profile_name=$(echo "$first_profile" | jq -r '.name')
      echo "$first_profile_id: $first_profile_name"

      # data (v3)
      data_response=$(curl -s -H "Authorization: $token_type $access_token" "https://www.googleapis.com/analytics/v3/data/ga/?ids=ga:${first_profile_id}&start-date=7daysAgo&end-date=today&metrics=ga:sessions")
      echo "$data_response" | jq '.'

      # data (v4)
      request_json=$(cat <<EOF
      {
        "reportRequests":
        [
          {
            "viewId": "${profile_id}",
            "dateRanges": [{"startDate": "2015-11-01", "endDate": "2016-11-30"}],
            "dimensions": [{"name":"ga:pagePath"}],
            "metrics": [{"expression": "ga:uniquePageviews"},{"expression":"ga:users"},{"expression":"ga:sessions"}],
            "filtersExpression": "ga:pagePath==/my/path"
          }
        ]
      }
      EOF
      )

      compact_request_json=$(echo "$request_json" | jq -c '.')

      # "Google Analytics Reporting API" must be enabled in API console:
      # https://console.developers.google.com/apis/api/analyticsreporting.googleapis.com/overview
      curl -X POST -H "Authorization: $token_type $access_token" -H 'Content-Type: application/json' --data-binary "$compact_request_json" https://analyticsreporting.googleapis.com/v4/reports:batchGet

  • Examples

    • API key
      OAuth 2.0
      Class

      Class
      types of client ID



      explained web
      installed
      service





      Python
      bash
      Python




      Using OAuth 2.0 for Web Server Applications
      Using OAuth 2.0 for Installed Applications

      Using OAuth 2.0 for Server to Server Applications









      Flow steps needed to go  through getting credentials

      • specified values
        • from oauth2client.client import OAuth2WebServerFlow
          flow = OAuth2WebServerFlow(client_id, client_secret, scope)
      • values from file
        • from oauth2client.client import flow_from_clientsecrets
          flow = flow_from_clientsecrets('client_secrets.json', scope=scope)





      Credentials contains key: refresh and access tokens for a single user

      • two steps
        • auth_uri = flow.step1_get_authorize_url()
          # Redirect the user to auth_uri on your platform.
          # authorization server redirects user to redirect_uri, with a code
          credentials = flow.step2_exchange(code)

      • from a given token
      • from storage
        • credentials = storage.get()
      • put credentials to storage
        • storage.put(credentials)
      • command line:
        • from oauth2client import tools
          credentials = tools.run_flow(flow, storage, tools.argparser.parse_args())
      credentials_path=/path/to/keyfile.json
      private_key=$(cat $credentials_path | jq -r '.private_key')

      # jwt header
      header=$(jq -c -n '{alg:"RS256",typ:"JWT"}')
      header_b64=$(echo -n $header | base64 | tr -d '\n' | tr -d '=' | tr '/+' '_-')


      # jwt claim set
      iss=$(cat $credentials_path | jq '.client_email')
      scope=\"https://www.googleapis.com/auth/analytics.readonly\"
      aud=\"https://www.googleapis.com/oauth2/v4/token\"
      iat=$(date +%s)
      validity_seconds=3600
      exp=$(( iat + validity_seconds ))

      claim_set=$(jq -c -n "{iss: $iss, scope: $scope, aud: $aud, exp: $exp, iat: $iat}")
      claim_set_b64=$(echo -n $claim_set | base64 | tr -d '\n' | tr -d '=' | tr '/+' '_-')

      header_claim_set=${header}.${claim_set}
      header_claim_set_b64=${header_b64}.${claim_set_b64}

      # jwt signature SHA256withRSA
      signature=$(echo -n "$header_claim_set_b64" | openssl dgst -sha256 -sign <(echo "$private_key" | sed 's/\\n/\n/g') | base64 | tr -d '\n' | tr -d '=' | tr '/+' '_-')

      # jwt
      jwt=${header_claim_set_b64}.${signature}

      # get token
      token_response=$(curl -s -H "Content-type: application/x-www-form-urlencoded" -X POST -d \
      "grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion=${jwt}" https://www.googleapis.com/oauth2/v4/token)

      token_type=$(echo "$token_response" | jq -r '.token_type')
      access_token=$(echo "$token_response" | jq -r '.access_token')

      • NOTE: client_email in json file (or provided when using p12) must have permissions in service administration (e.g. analytics.google.com -> Administrador -> Gestionar usuaris)
      • from json file (recommended)
        • from oauth2client.service_account import ServiceAccountCredentials

          scopes = ['https://www.googleapis.com/auth/analytics.readonly']

          credentials = ServiceAccountCredentials.from_json_keyfile_name(
              '/path/to/keyfile.json', scopes)
      • from p12 file
        • from oauth2client.service_account import ServiceAccountCredentials

          client_email = '123456789000-abc123def456@developer.gserviceaccount.com'
          private_key_password = 'notasecret'

          scopes = ['https://www.googleapis.com/auth/sqlservice.admin']

          credentials = ServiceAccountCredentials.from_p12_keyfile(
              client_email, '/path/to/keyfile.p12', private_key_password, scopes)



      Storage contains credentials
      • contains single credentials
        • from oauth2client.file import Storage
          storage = Storage('credentials.dat')
      • contains several credentials
      • ...


      http




      • import httplib2
        http = httplib2.Http()
        http_auth = credentials.authorize(http)

      service
      service = build('books', 'v1', developerKey=api_key)



      service = build('calendar', 'v3', http=http_auth)

      • analytics v3
        • service = build('analytics', 'v3', http=http_auth)
      • analytics v4
        • DISCOVERY_URI = ('https://analyticsreporting.googleapis.com/$discovery/rest')

          analytics = build('analytics', 'v4', http=http, discoveryServiceUrl=DISCOVERY_URI)

      collection
      collection = service.volumes()



      request = service.events().list(calendarId='primary')
      • management
        • list of accounts
          • accounts_response=$(curl -s -H "Authorization: $token_type $access_token" https://www.googleapis.com/analytics/v3/management/accounts)
        • list of properties
          • properties_response=$(curl -s -H "Authorization: $token_type $access_token" https://www.googleapis.com/analytics/v3/management/accounts/${first_account_id}/webproperties)
        • list of profiles
          • profiles_response=$(curl -s -H "Authorization: $token_type $access_token" https://www.googleapis.com/analytics/v3/management/accounts/${first_account_id}/webproperties/${first_property_id}/profiles)
      • analytics
        • analytics v3
          • data_response=$(curl -s -H "Authorization: $token_type $access_token" "https://www.googleapis.com/analytics/v3/data/ga/?ids=ga:${profile_id}&start-date=7daysAgo&end-date=today&metrics=ga:sessions")
        • realtime v3:
          • data_response=$(curl -s -H "Authorization: $token_type $access_token" "https://www.googleapis.com/analytics/v3/data/realtime/?ids=ga:${profile_id}&metrics=rt:activeUsers&dimensions=rt:medium")
        • analytics v4
          • request_json=$(cat <<EOF
            {
              "reportRequests":
              [
                {
                  "viewId": "${profile_id}",
                  "dateRanges": [{"startDate": "2015-11-01", "endDate": "2016-11-30"}],
                  "dimensions": [{"name":"ga:pagePath"}],
                  "metrics": [{"expression": "ga:uniquePageviews"},{"expression":"ga:users"},{"expression":"ga:sessions"}],
                  "filtersExpression": "ga:pagePath==/my/path"
                }
              ]
            }
            EOF
            )

            compact_request_json=$(echo "$request_json" | jq -c '.')

            # "Google Analytics Reporting API" must be enabled in API console:
            # https://console.developers.google.com/apis/api/analyticsreporting.googleapis.com/overview
            data_response=$(curl -s -X POST -H "Authorization: $token_type $access_token" -H 'Content-Type: application/json' --data-binary "$compact_request_json" https://analyticsreporting.googleapis.com/v4/reports:batchGet)
      • list of accounts
        • response = service.management().accounts().list().execute()
      • analytics v3: number of sessions (IMPORTANT: use analytics v4 instead)
        • number_sessions = service.data().ga().get(
                ids='ga:' + profile_a_0_i_id,
                start_date='7daysAgo',
                end_date='today',
                metrics='ga:sessions').execute()
      • realtime v3:
        • if relative_path:
              response =
          service.data().realtime().get(
                                 ids='ga:' + profile_id,
                                 metrics='rt:activeUsers',
                                 dimensions='rt:pagePath',
                                 filters='rt:pagePath=={}'.format(relative_path) ).execute()
          else:
              response =
          service.data().realtime().get(
                                 ids='ga:' + profile_id,
                                 metrics='rt:activeUsers',
                                 dimensions='rt:pagePath').execute()
                
          results = response.get('totalsForAllResults')
          number_active_users = results.get('rt:activeUsers', 0)

      • analytics v4
        • view_id = 'zzzzzzzzz'
          body={
                  'reportRequests': [
                  {
                    'viewId': view_id,
                    'dateRanges': [{'startDate': '2015-11-01', 'endDate': 'today'}],
                    'metrics': [{'expression': 'ga:uniquePageviews'},{'expression':'ga:users'},{'expression':'ga:sessions'}],
                    'filtersExpression': 'ga:pagePath==/my/path'
                  }]
          }
          response =
          service.reports().batchGet( body=body ).execute()
          results = simple_results_from_report(response)
          number_users = results.get('ga:users', 0)

      • realtime v4
        • ...



      request
      request = collection.list(source='public', q='android')


      response
      response = request.execute()



      response = request.execute()





      • print json.dumps( response )
      • import pprint
        pprint.pprint(response)



  • Google Analytics
    • Comptes /
      Accounts
      Propietats / Properties
      Mostres /
      Views
      id
      URL (selfLink)
      bash
      Python
      https://www.googleapis.com/analytics/v3/...
      [...]
      token_type=$(echo "$token_response" | jq -r '.token_type')
      access_token=$(echo "$token_response" | jq -r '.access_token')
      scopes = ['https://www.googleapis.com/auth/analytics.readonly']
      credentials = ...
      http_auth = credentials.authorize(Http())
      service = build('analytics', 'v3', http=http_auth)
      Accounts

      management/accounts
      accounts_response=$(curl -s -H "Authorization: $token_type $access_token" https://www.googleapis.com/analytics/v3/management/accounts) accounts = service.management().accounts().list().execute()
      account_a

      xxxxxxxx
      management/accounts/xxxxxxxx account_a=$(echo "$accounts_response" | jq '.items[0]')
      account_a_id=$(echo "$account_a" | jq -r '.id')
      account_a_name=$(echo "$account_a" | jq -r '.name')

      account_a = accounts.get('items')[0]
      Properties

      management/accounts/xxxxxxxx/webproperties properties_response=$(curl -s -H "Authorization: $token_type $access_token" https://www.googleapis.com/analytics/v3/management/accounts/${account_a_id}/webproperties)
      sites_a = service.management().webproperties().list(accountId=xxxxxxxx).execute()
      site_a_0

      UA-xxxxxxxx-1
      management/accounts/xxxxxxxx/webproperties/UA-xxxxxxxx-1 site_a_0=$(echo "$properties_response" | jq '.items[0]')
      site_a_0_id=$(echo "$site_a_0" | jq -r '.id')
      site_a_0_name=$(echo "$site_a_0" | jq -r '.name')

      site_a_0 = sites_a.get('items')[0]
      Views


      management/accounts/xxxxxxxx/webproperties/UA-xxxxxxxx-1/profiles profiles_response=$(curl -s -H "Authorization: $token_type $access_token" https://www.googleapis.com/analytics/v3/management/accounts/${account_a_id}/webproperties/${site_a_0_id}/profiles)
      profiles_a_0 = service.management().profiles().list(accountId=xxxxxxxx,webPropertyId=UA-xxxxxxxx-1).execute()
      profile_a_0_i
      (e.g. "Totes les dades de llocs web")
      zzzzzzzzz
      /data/ga/?ids=ga:${profile_a_0_i_id}&start-date=7daysAgo&end-date=today&metrics=ga:sessions
      profile_a_0_i=$(echo "$profiles_response" | jq '.items[0]')
      profile_a_0_i_id=$(echo "$first_profile" | jq -r '.id')
      profile_a_0_i_name=$(echo "$first_profile" | jq -r '.name')
      profile_id=$profile_a_0_i_id

      profile_a_0_i = profiles_a_0.get('items')[0]
      profile_a_0_i_id = profile_a_0_i.get('id')
      number_sessions = service.data().ga().get(
            ids='ga:' + profile_a_0_i_id,
            start_date='7daysAgo',
            end_date='today',
            metrics='ga:sessions').execute()

      profile_a_0_ii


      view_a_0_ii = profiles_a_0.get('items')[1]
      ...




      site_a_1


      UA-xxxxxxxx-2


      ...






      account_b




      yyyyyyyy
      management/accounts/yyyyyyyy
      account_b = accounts.get('items')[1]
      Properties







      site_b_0






      Views





      profile_b_0_i




      profile_b_0_ii



      ...




      site_b_1





      ...






      ...








http://www.francescpinyol.cat/google.html
Primera versió: / First version: 15.XI.2016
Darrera modificació: 12 de gener de 2019 / Last update: 12th January 2019

Valid HTML 4.01!

Cap a casa / Back home