PyGoogleDM – Access the Google Analytics API with Python

Extract Data from the Google Analytics API via Python and Insert it into a MySQL database

This Python program extracts data from the Google Analytics API and inserts it into a MySQL database. The utility can be installed by extracting the contents of the to the desired location and running from the command line. Make sure the pre-requisite Python modules listed below have been installed. This program was originally written on Python v2.6.5.

Pre-requisite Python Modules

Run “” from the command line as follows:

user@host:~/google_analytics/ga_data/src$ ./

“” has a number of run-time options:

Main prompt selection:
  • 1 = Add new records ONLY to MySQL database
  • 2 = Add new records & update existing records in MySQL database
  • 3 = Output CSV files (page,site_stats,visitor) containing data retrieved
  • 4 = Output SQL script containing INSERT/UPDATE statements for bulk load
Extracting Data from the Google Analytics API

Command window for execution

Additional run-time options:
  • Change home directory and reload config
  • Drop & recreate ga_dm database before program execution
  • Truncate all tables before program execution
  • Obfuscate website and page data in MySQL database after program execution
Sourcecode for main() in python program
ga = ga_data()
set_paths() # set initial directory paths
ga.load_config() # set up environment and load configuration
ga.main_prompt(action) # prompt user for action
ga.get_accounts() # retrieve google analytics data
db = mysql()
db.connect(ga) # establish connection to mysql server
if ga.prompts['create']: # option to recreate ga_dm database"Dropping and recreating ga_dm database")
if ga.prompts['truncate']: # option to truncate ga_dm database"Truncating data in ga_dm database")
for account in ga.accounts: # perform for each account
	''' date table must always be checked and updated if necessary '''
	''' site table must always be checked and updated if necessary '''
	ga.get_data(account,'site') # retrieve site stats
	if ga.prompts['action'] is action.csv: # output to csv if desired
	else: # otherwise update site stats table
	ga.get_data(account,'page') # retrieve page stats
	if ga.prompts['action'] is action.csv: # output to csv if desired
	else: # otherwise update page table
	ga.get_data(account,'visitor') # retrieve visitor stats
	if ga.prompts['action'] is action.csv: # output to csv if desired
	else: # otherwise update visitor table
if ga.prompts['obfuscate']: # option to obfuscate ga_dm database"Obfuscating website and page data in ga_dm database")
	print "Website and page data obfuscated."
ga.finish() # flush writes and close open file handles

The file download contains a ga_data directory with all required files (except Python pre-requisite modules noted in README file). Follow instructions in README.txt to set up and run.