public
Last active

The most basic Google Analytics access in python

  • Download Gist
basic-gdata.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
import gdata.analytics.service
from datetime import date, timedelta
from xml.dom.minidom import parseString
 
def main():
 
client = gdata.analytics.service.AnalyticsDataService()
client.ClientLogin('<your email here>', '<your password here>')
 
today = date.today()
q = gdata.analytics.service.DataQuery(
ids='ga:<your GA ID here>',
dimensions='ga:pagePath',
metrics='ga:pageviews',
filters='ga:pagepath=~^/store',
sort='-ga:pageviews',
start_date=today - timedelta(days=7),
end_date=today,
start_index='',
max_results='50')
gadata = client.AnalyticsDataFeed(q.ToUri())
xml = parseString(str(gadata))
for title in xml.getElementsByTagName("ns0:title"):
for child in title.childNodes:
print child.data
if __name__ == "__main__":
main()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.