Created
September 21, 2014 09:22
-
-
Save yuvadm/be9bcfce52c7231b2ee4 to your computer and use it in GitHub Desktop.
Python Google Analytics full server-side integration flow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import httplib2 | |
from apiclient.discovery import build | |
from oauth2client.client import SignedJwtAssertionCredentials | |
def build_google_analytics_service(): | |
http = httplib2.Http() | |
credentials = SignedJwtAssertionCredentials( | |
GOOGLE_CLIENT_EMAIL, | |
GOOGLE_PRIVATE_KEY, | |
'https://www.googleapis.com/auth/analytics.readonly') | |
http = credentials.authorize(http) | |
return build('analytics', 'v3', http=http) | |
def get_first_profile_id(service): | |
accounts = service.management().accounts().list().execute() | |
firstAccountId = accounts.get('items')[0].get('id') | |
webproperties = service.management().webproperties().list( | |
accountId=firstAccountId).execute() | |
firstWebpropertyId = webproperties.get('items')[0].get('id') | |
profiles = service.management().profiles().list( | |
accountId=firstAccountId, webPropertyId=firstWebpropertyId).execute() | |
return profiles.get('items')[0].get('id') | |
if __name__ == '__main__': | |
service = build_google_analytics_service() | |
profile = get_first_profile_id(service) | |
# ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment