Last active
August 29, 2015 14:08
-
-
Save geramirez/c179ed6e726be6c7a3b9 to your computer and use it in GitHub Desktop.
GA-OauthFlow
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
request=service.data().ga().get( | |
ids='ga:' + "65544933", | |
start_date='2014-10-01', | |
end_date='2014-10-21', | |
metrics='ga:pageviews,ga:uniquePageviews', | |
dimensions='ga:region,ga:date', | |
filters="ga:country==United States").execute() | |
#import deps. | |
import pandas | |
#z-score function | |
z_norm = lambda series: (series - series.mean()) / series.std() | |
#extract columns | |
headers = [element['name'] for element in request['columnHeaders']] | |
#load dataset | |
df = pandas.DataFrame(request['rows'], columns=headers) | |
#create a datetime col | |
df['ga:date'] = pandas.to_datetime(df['ga:date'].apply(lambda row: str(row))) | |
#set the index | |
df = df.set_index(df['ga:date'],drop=False) | |
#make the variables numberic | |
df['ga:pageviews'] = df['ga:pageviews'].astype(int) | |
df_pivot=df.pivot_table(rows="ga:date", cols= "ga:region",values="ga:pageviews",aggfunc="mean") | |
#normalize on columns | |
df_pivot=df_pivot.apply(z_norm,axis=0) | |
df_pivot.iloc[:,0:5].plot(figsize=(14, 6)) |
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 oauth2client.client import flow_from_clientsecrets | |
from oauth2client.client import OAuth2WebServerFlow | |
from apiclient.discovery import build | |
from oauth2client.file import Storage | |
from oauth2client.tools import run | |
TOKEN_FILE_NAME = 'analytics.dat' | |
CLIENT_SECRETS = 'ramirezg_key.json' | |
MISSING_CLIENT_SECRETS_MESSAGE = '%s is missing' % CLIENT_SECRETS | |
FLOW = flow_from_clientsecrets(CLIENT_SECRETS, | |
scope='https://www.googleapis.com/auth/analytics', | |
message=MISSING_CLIENT_SECRETS_MESSAGE, | |
redirect_uri = 'http://localhost:5000/oauth2callback') | |
def prepare_credentials(): | |
storage = Storage(TOKEN_FILE_NAME) | |
print "got here" | |
credentials = storage.get() | |
if credentials is None or credentials.invalid: | |
credentials = run(FLOW, storage) | |
return credentials | |
def initialize_service(): | |
http = httplib2.Http() | |
#Get stored credentials or run the Auth Flow if none are found | |
credentials = prepare_credentials() | |
http = credentials.authorize(http) | |
#Construct and return the authorized Analytics Service Object | |
return build('analytics', 'v3', http=http) | |
service=initialize_service() | |
request=service.data().ga().get( | |
ids='ga:' + "65544933", | |
start_date='2014-10-01', | |
end_date='2014-10-21', | |
metrics='ga:pageviews,ga:uniquePageviews', | |
dimensions='ga:region,ga:date', | |
filters="ga:country==United States").execute() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment