Skip to main content
Help the Python Software Foundation raise $60,000 USD by December 31st!  Building the PSF Q4 Fundraiser

Simple Google Analytics API data extraction.

Project description

Freesixty

A simple Google Analytics API data extraction.

Installation

pip install freesixty

Access credentials

To set up access to your Google Analytics follow first step of these instructions. Store them in your local machine and enter their path into KEY_FILE_LOCATION variable.

Get data

import freesixty

KEY_FILE_LOCATION = './client_secrets.json'
VIEW_ID = 'XXXXXXX'

query = {
    'reportRequests': [
    {
        'viewId': VIEW_ID,
        'dateRanges': [{'startDate': '2009-01-01', 'endDate': '2019-01-05'}],
        'metrics': [{'expression': 'ga:sessions'}],
        'dimensions': [{'name': 'ga:country', 'name': 'ga:date'}]
    }]
}

analytics = freesixty.initialize_analyticsreporting(KEY_FILE_LOCATION)
result, is_data_golden = freesixty.execute_query(analytics, query)

On the other hand if we want to store resulting data to a desired URI.

import freesixty

KEY_FILE_LOCATION = './client_secrets.json'
VIEW_ID = 'XXXXXXX'
folder_uri = 'file:///tmp/example/folder'

query = {
    'reportRequests': [
    {
        'viewId': VIEW_ID,
        'dateRanges': [{'startDate': '2009-01-01', 'endDate': '2019-01-05'}],
        'metrics': [{'expression': 'ga:sessions'}],
        'dimensions': [{'name': 'ga:country', 'name': 'ga:date'}]
    }]
}

analytics = freesixty.initialize_analyticsreporting(KEY_FILE_LOCATION)
freesixty.store_query(analytics, query, folder_uri)

Getting more data

In case a query would return over 100k rows of data it will fail. We can get around it by splitting the date range into smaller chunks:

queries = freesixty.split_query(query=query, start_date='2019-01-01', end_date='2019-02-01', freq='D')

for query in queries:
    freesixty.store_query(analytics, query, folder_uri)

Useful links

TODO:

  • More complete tests

:cake:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for freesixty, version 0.1.32
Filename, size File type Python version Upload date Hashes
Filename, size freesixty-0.1.32-py2.py3-none-any.whl (5.4 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size freesixty-0.1.32.tar.gz (7.1 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page