Skip to content

Instantly share code, notes, and snippets.

@jrjames83
Created August 12, 2016 18:18
Show Gist options
  • Save jrjames83/a5f8838448765d4b14332b41eb3eb3ff to your computer and use it in GitHub Desktop.
Save jrjames83/a5f8838448765d4b14332b41eb3eb3ff to your computer and use it in GitHub Desktop.
python script to get ga data
import argparse
import csv
import os.path
import datetime
import time
from datetime import date, timedelta as td
from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
import httplib2
from oauth2client import client
from oauth2client import file
from oauth2client import tools
def get_service(api_name, api_version, scope, key_file_location,
service_account_email):
credentials = ServiceAccountCredentials.from_p12_keyfile(
service_account_email, key_file_location, scopes=scope)
http = credentials.authorize(httplib2.Http())
service = build(api_name, api_version, http=http)
return service
def get_results(service, start, end):
return service.data().ga().get(
ids='ga:98400179',
max_results='10000',
start_date=start,
end_date=end,
dimensions='ga:transactionId, ga:sourceMedium, ga:campaign',
metrics='ga:transactions',
#sort='-ga:sessions'
#filters='ga:medium==referral'
).execute()
timestr = time.strftime('%b-%d-%Y_%H%M')
def write_file(results):
file_exists = os.path.isfile('%s.csv' % timestr)
contains_sampled = results['containsSampledData']
nbr_rows = results['totalResults']
dims = list(results['query']['dimensions'].split(","))
metrs = list(results['query']['metrics'])
cols = dims + metrs + ["sampled"] + ["rows_returned"] + ["row_nbr"]
with open('%s.csv' % timestr, 'a') as csvfile:
mywriter = csv.writer(csvfile, delimiter='\t', quotechar='"',
quoting=csv.QUOTE_MINIMAL, lineterminator='\n')
if not file_exists:
mywriter.writerow(cols)
for idx, row in enumerate(results['rows']):
to_write = [s.encode('utf-8') for s in row]
mywriter.writerow(to_write+[contains_sampled, nbr_rows, idx+1])
def main():
scope = ['https://www.googleapis.com/auth/analytics.readonly']
service_account_email = '#######'
key_file_location = 'keydata.p12'
service = get_service('analytics', 'v3', scope, key_file_location,
service_account_email)
#Set-up Dates for Extraction
start = date(2015, 5, 1)
end = date(2016, 7, 31)
delta = end - start
dates = [(start + td(days=i)).isoformat() for i in range(delta.days + 1)]
#Error Logging
text_file = open("test.txt", "a")
#Proccess and Write Data
for idx, x in enumerate(dates):
try:
data = get_results(service, x, x)
write_file(data)
print "just wrote %s %s" % (idx, x)
text_file.write("just wrote %s for %s \n" % (idx, x))
except Exception, e:
print str(e), x
text_file.write("just failed %s %s for %s \n" % (str(e), idx, x))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment