Skip to content

Instantly share code, notes, and snippets.

@epicserve
Last active December 22, 2015 10:09
Show Gist options
  • Select an option

  • Save epicserve/6456150 to your computer and use it in GitHub Desktop.

Select an option

Save epicserve/6456150 to your computer and use it in GitHub Desktop.

Revisions

  1. epicserve revised this gist Sep 6, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions list_ga_top_pages.py
    Original file line number Diff line number Diff line change
    @@ -2,8 +2,8 @@
    List the top pages for a website using Google Analytics. This is a base
    example that you could build off of to create lists on your website like,
    "Top Articles this Week", etc.
    See the `README.rst`_ for more information.
    See the README.rst documentation for more information.
    """

    from apiclient.discovery import build
  2. epicserve revised this gist Sep 6, 2013. 2 changed files with 63 additions and 55 deletions.
    61 changes: 61 additions & 0 deletions README.rst
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    README
    ======

    List the top pages for a website using Google Analytics. This is a base
    example that you could build off of to create lists on your website like,
    "Top Articles this Week", etc.

    Installation
    ------------

    1. Install the required python libraries::

    pip install google-api-python-client==1.2
    pip install PyOpenSSL==0.13.1

    2. Go to https://code.google.com/apis/console/ to get your API credentials.

    - Click create project
    - Click the on/off button next to "Analytics API" to turn on
    "Analytics API" service.
    - Click on the "API Access" tab
    - Click the "Create an OAuth 2.0 client ID" button
    - Add a "Product name" and "Home Page URL" and then click next
    - Select "Service Account"
    - Click "Create client ID"
    - Click the "Download private key" button
    - Rename the file google-api-private-key.p12 and move it into the same
    directory as this script

    4. Make note of your service account email address
    (e.g. 1212232334344545@developer.gserviceaccount.com).
    Log in to Google Analytics account and then give your service account email
    address read access to the web site property you want to use this script for.


    Helpful Links
    -------------

    - `API Console <https://code.google.com/apis/console>`_
    - `Service Accounts <https://developers.google.com/accounts/docs/OAuth2ServiceAccount>`_
    - `Using OAuth 2.0 for Server to Server Applications <https://developers.google.com/accounts/docs/OAuth2ServiceAccount>`_
    - `Service Account Example <https://code.google.com/p/google-api-python-client/source/browse/samples/service_account/tasks.py>`_


    Usage
    -----

    Before you run the script you'll need to get the property ID for the website
    you want to get top pages for. Login to Google Analytics and then view the
    analytics for the website you want the top pages for. The property ID will be
    the number after the "p" in the URL.

    If the URL was
    https://www.google.com/analytics/web/?hl=en&pli=1#report/visitors-overview/a1212121w23232323p34343434/,
    then your profile ID would be 34343434.

    Run the script::

    python list_ga_top_pages.py --profile_id 34343434 \
    --service_account_email 1212232334344545@developer.gserviceaccount.com \
    --start_date '2013-09-01' --end_date '2013-09-05' --filter '^/news/201*' --max_results 20
    57 changes: 2 additions & 55 deletions list_ga_top_pages.py
    Original file line number Diff line number Diff line change
    @@ -3,62 +3,9 @@
    example that you could build off of to create lists on your website like,
    "Top Articles this Week", etc.
    Installation
    ------------
    1. Install the required python libraries::
    pip install google-api-python-client==1.2
    pip install PyOpenSSL==0.13.1
    2. Go to https://code.google.com/apis/console/ to get your API credentials.
    - Click create project
    - Click the on/off button next to "Analytics API" to turn on
    "Analytics API" service.
    - Click on the "API Access" tab
    - Click the "Create an OAuth 2.0 client ID" button
    - Add a "Product name" and "Home Page URL" and then click next
    - Select "Service Account"
    - Click "Create client ID"
    - Click the "Download private key" button
    - Rename the file google-api-private-key.p12 and move it into the same
    directory as this script
    4. Make note of your service account email address
    (e.g. 1212232334344545@developer.gserviceaccount.com).
    Log in to Google Analytics account and then give your service account email
    address read access to the web site property you want to use this script for.
    Helpful Links
    -------------
    - `API Console <https://code.google.com/apis/console>`_
    - `Service Accounts <https://developers.google.com/accounts/docs/OAuth2ServiceAccount>`_
    - `Using OAuth 2.0 for Server to Server Applications <https://developers.google.com/accounts/docs/OAuth2ServiceAccount>`_
    - `Service Account Example <https://code.google.com/p/google-api-python-client/source/browse/samples/service_account/tasks.py>`_
    Usage
    -----
    Before you run the script you'll need to get the property ID for the website
    you want to get top pages for. Login to Google Analytics and then view the
    analytics for the website you want the top pages for. The property ID will be
    the number after the "p" in the URL.
    If the URL was
    https://www.google.com/analytics/web/?hl=en&pli=1#report/visitors-overview/a1212121w23232323p34343434/,
    then your profile ID would be 34343434.
    Run the script::
    python list_ga_top_pages.py --profile_id 34343434 \
    --service_account_email 1212232334344545@developer.gserviceaccount.com \
    --start_date '2013-09-01' --end_date '2013-09-05' --filter '^/news/201*' --max_results 20
    See the `README.rst`_ for more information.
    """

    from apiclient.discovery import build
    from oauth2client.client import SignedJwtAssertionCredentials

  3. epicserve revised this gist Sep 6, 2013. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion list_ga_top_pages.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    """
    List the top pages for a website using Google Analytics.
    List the top pages for a website using Google Analytics. This is a base
    example that you could build off of to create lists on your website like,
    "Top Articles this Week", etc.
    Installation
    ------------
  4. epicserve revised this gist Sep 6, 2013. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion list_ga_top_pages.py
    Original file line number Diff line number Diff line change
    @@ -62,7 +62,6 @@

    import argparse
    import httplib2
    import os

    __author__ = "epicserve@gmail.com (Brent O'Connor)"

  5. epicserve revised this gist Sep 6, 2013. 1 changed file with 18 additions and 18 deletions.
    36 changes: 18 additions & 18 deletions list_ga_top_pages.py
    Original file line number Diff line number Diff line change
    @@ -11,31 +11,31 @@
    2. Go to https://code.google.com/apis/console/ to get your API credentials.
    - Click create project
    - Click the on/off button next to "Analytics API" to turn on
    "Analytics API" service.
    - Click on the "API Access" tab
    - Click the "Create an OAuth 2.0 client ID" button
    - Add a "Product name" and "Home Page URL" and then click next
    - Select "Service Account"
    - Click "Create client ID"
    - Click the "Download private key" button
    - Rename the file google-api-private-key.p12 and move it into the same
    directory as this script
    - Click create project
    - Click the on/off button next to "Analytics API" to turn on
    "Analytics API" service.
    - Click on the "API Access" tab
    - Click the "Create an OAuth 2.0 client ID" button
    - Add a "Product name" and "Home Page URL" and then click next
    - Select "Service Account"
    - Click "Create client ID"
    - Click the "Download private key" button
    - Rename the file google-api-private-key.p12 and move it into the same
    directory as this script
    4. Make note of your service account email address
    (e.g. 1212232334344545@developer.gserviceaccount.com).
    Log in to Google Analytics account and then give your service account email
    address read access to the web site property you want to use this script for.
    (e.g. 1212232334344545@developer.gserviceaccount.com).
    Log in to Google Analytics account and then give your service account email
    address read access to the web site property you want to use this script for.
    Helpful Links
    -------------
    - `API Console <https://code.google.com/apis/console>`_
    - `Service Accounts <https://developers.google.com/accounts/docs/OAuth2ServiceAccount>`_
    - `Using OAuth 2.0 for Server to Server Applications <https://developers.google.com/accounts/docs/OAuth2ServiceAccount>`_
    - `Service Account Example <https://code.google.com/p/google-api-python-client/source/browse/samples/service_account/tasks.py>`_
    - `API Console <https://code.google.com/apis/console>`_
    - `Service Accounts <https://developers.google.com/accounts/docs/OAuth2ServiceAccount>`_
    - `Using OAuth 2.0 for Server to Server Applications <https://developers.google.com/accounts/docs/OAuth2ServiceAccount>`_
    - `Service Account Example <https://code.google.com/p/google-api-python-client/source/browse/samples/service_account/tasks.py>`_
    Usage
  6. epicserve revised this gist Sep 6, 2013. 1 changed file with 48 additions and 59 deletions.
    107 changes: 48 additions & 59 deletions list_ga_top_pages.py
    Original file line number Diff line number Diff line change
    @@ -4,9 +4,10 @@
    Installation
    ------------
    1. Install the required python library::
    1. Install the required python libraries::
    pip install google-api-python-client==1.2
    pip install PyOpenSSL==0.13.1
    2. Go to https://code.google.com/apis/console/ to get your API credentials.
    @@ -16,16 +17,31 @@
    - Click on the "API Access" tab
    - Click the "Create an OAuth 2.0 client ID" button
    - Add a "Product name" and "Home Page URL" and then click next
    - Select "Installed application"
    - Select "Service Account"
    - Click "Create client ID"
    - Click the "Download private key" button
    - Rename the file google-api-private-key.p12 and move it into the same
    directory as this script
    3. Download client_secrets.json and put it in the same directroy as this script
    4. Make note of your service account email address
    (e.g. 1212232334344545@developer.gserviceaccount.com).
    Log in to Google Analytics account and then give your service account email
    address read access to the web site property you want to use this script for.
    Helpful Links
    -------------
    - `API Console <https://code.google.com/apis/console>`_
    - `Service Accounts <https://developers.google.com/accounts/docs/OAuth2ServiceAccount>`_
    - `Using OAuth 2.0 for Server to Server Applications <https://developers.google.com/accounts/docs/OAuth2ServiceAccount>`_
    - `Service Account Example <https://code.google.com/p/google-api-python-client/source/browse/samples/service_account/tasks.py>`_
    Usage
    -----
    Before you run the script you'll need to get the profile id for the website
    Before you run the script you'll need to get the property ID for the website
    you want to get top pages for. Login to Google Analytics and then view the
    analytics for the website you want the top pages for. The property ID will be
    the number after the "p" in the URL.
    @@ -36,66 +52,40 @@
    Run the script::
    python list_ga_top_pages.py --profile_id 34343434 --start_date '2013-09-01' \
    --end_date '2013-09-05' --filter '^/news/201*' --max_results 20
    python list_ga_top_pages.py --profile_id 34343434 \
    --service_account_email 1212232334344545@developer.gserviceaccount.com \
    --start_date '2013-09-01' --end_date '2013-09-05' --filter '^/news/201*' --max_results 20
    """
    from apiclient import discovery
    from oauth2client import client
    from oauth2client import file
    from oauth2client import tools
    from apiclient.discovery import build
    from oauth2client.client import SignedJwtAssertionCredentials

    import sys
    import argparse
    import httplib2
    import os

    __author__ = "epicserve@gmail.com (Brent O'Connor)"


    def get_service(argv, name='analytics', version='v3', doc=None, client_secrets=None, scope='https://www.googleapis.com/auth/analytics.readonly', parents=[]):

    if scope is None:
    scope = 'https://www.googleapis.com/auth/' + name

    if doc is None:
    doc = __doc__

    # Parser command-line arguments.
    parent_parsers = [tools.argparser]
    parent_parsers.extend(parents)
    parser = argparse.ArgumentParser(
    description=doc,
    formatter_class=argparse.RawDescriptionHelpFormatter,
    parents=parent_parsers)
    flags = parser.parse_args(argv[1:])
    def get_ga_service(
    service_account_email,
    pk_file_path,
    scope='https://www.googleapis.com/auth/analytics.readonly'):

    # Name of a file containing the OAuth 2.0 information for this
    # application, including client_id and client_secret, which are found
    # on the API Access tab on the Google APIs
    # Console <http://code.google.com/apis/console>.
    if client_secrets is None:
    client_secrets = os.path.join(os.path.dirname(__file__), 'client_secrets.json')
    # Load the key in PKCS 12 format private key
    f = open(pk_file_path, 'rb')
    key = f.read()
    f.close()

    # Set up a Flow object to be used if we need to authenticate.
    flow = client.flow_from_clientsecrets(
    client_secrets,
    scope=scope,
    message=tools.message_if_missing(client_secrets))
    credentials = SignedJwtAssertionCredentials(
    service_account_email,
    key,
    scope='https://www.googleapis.com/auth/analytics.readonly')
    http = credentials.authorize(httplib2.Http())

    # Prepare credentials, and authorize HTTP object with them.
    # If the credentials don't exist or are invalid run through the native client
    # flow. The Storage object will ensure that if successful the good
    # credentials will get written back to a file.
    storage = file.Storage(name + '.dat')
    credentials = storage.get()
    if credentials is None or credentials.invalid:
    credentials = tools.run_flow(flow, storage, flags)
    http = credentials.authorize(http=httplib2.Http())
    service = build("analytics", "v3", http=http)

    # Construct a service object via the discovery service.
    service = discovery.build(name, version, http=http)
    return (service, flags)
    return service


    def get_top_pages(service, profile_id, start_date, end_data, filter='^/*', max_results=50):
    @@ -113,18 +103,17 @@ def get_top_pages(service, profile_id, start_date, end_data, filter='^/*', max_r

    if __name__ == '__main__':

    parser = argparse.ArgumentParser(description='List the top pages for a website.', add_help=False)
    parser = argparse.ArgumentParser(description='List the top pages for a website.')
    parser.add_argument('-pid', '--profile_id', type=int, required=True)
    parser.add_argument('--start_date', required=True, help='2013-09-01')
    parser.add_argument('--end_date', required=True, help='2013-09-05')
    parser.add_argument('--filter', default='^/*', help='^/news/201*')
    parser.add_argument('--service_account_email', required=True)
    parser.add_argument('--start_date', required=True, help='YYYY-MM-DD')
    parser.add_argument('--end_date', required=True, help='YYYY-MM-DD')
    parser.add_argument('--filter', default='^/*', help='^/news/20*')
    parser.add_argument('--max_results', default=50, type=int)
    argv = list(sys.argv)
    if '--noauth_local_webserver' in argv:
    argv.pop(argv.index('--noauth_local_webserver'))
    args = parser.parse_args(argv[1:])
    parser.add_argument('--pk_file_path', default='google-api-private-key.p12')
    args = parser.parse_args()

    service, flags = get_service(sys.argv, parents=[parser])
    service = get_ga_service(args.service_account_email, args.pk_file_path)
    results = get_top_pages(service, args.profile_id, start_date=args.start_date, end_data=args.end_date, filter=args.filter, max_results=args.max_results)

    if results.get('rows', []):
  7. epicserve revised this gist Sep 5, 2013. 1 changed file with 11 additions and 4 deletions.
    15 changes: 11 additions & 4 deletions list_ga_top_pages.py
    Original file line number Diff line number Diff line change
    @@ -53,11 +53,14 @@
    __author__ = "epicserve@gmail.com (Brent O'Connor)"


    def get_service(argv, name, version, doc, filename, scope=None, parents=[]):
    def get_service(argv, name='analytics', version='v3', doc=None, client_secrets=None, scope='https://www.googleapis.com/auth/analytics.readonly', parents=[]):

    if scope is None:
    scope = 'https://www.googleapis.com/auth/' + name

    if doc is None:
    doc = __doc__

    # Parser command-line arguments.
    parent_parsers = [tools.argparser]
    parent_parsers.extend(parents)
    @@ -71,7 +74,8 @@ def get_service(argv, name, version, doc, filename, scope=None, parents=[]):
    # application, including client_id and client_secret, which are found
    # on the API Access tab on the Google APIs
    # Console <http://code.google.com/apis/console>.
    client_secrets = os.path.join(os.path.dirname(filename), 'client_secrets.json')
    if client_secrets is None:
    client_secrets = os.path.join(os.path.dirname(__file__), 'client_secrets.json')

    # Set up a Flow object to be used if we need to authenticate.
    flow = client.flow_from_clientsecrets(
    @@ -115,9 +119,12 @@ def get_top_pages(service, profile_id, start_date, end_data, filter='^/*', max_r
    parser.add_argument('--end_date', required=True, help='2013-09-05')
    parser.add_argument('--filter', default='^/*', help='^/news/201*')
    parser.add_argument('--max_results', default=50, type=int)
    args = parser.parse_args()
    argv = list(sys.argv)
    if '--noauth_local_webserver' in argv:
    argv.pop(argv.index('--noauth_local_webserver'))
    args = parser.parse_args(argv[1:])

    service, flags = get_service(sys.argv, 'analytics', 'v3', __doc__, __file__, scope='https://www.googleapis.com/auth/analytics.readonly', parents=[parser])
    service, flags = get_service(sys.argv, parents=[parser])
    results = get_top_pages(service, args.profile_id, start_date=args.start_date, end_data=args.end_date, filter=args.filter, max_results=args.max_results)

    if results.get('rows', []):
  8. epicserve revised this gist Sep 5, 2013. 1 changed file with 10 additions and 1 deletion.
    11 changes: 10 additions & 1 deletion list_ga_top_pages.py
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,16 @@
    pip install google-api-python-client==1.2
    2. Go to https://code.google.com/apis/console/ to get your API credentials
    2. Go to https://code.google.com/apis/console/ to get your API credentials.
    - Click create project
    - Click the on/off button next to "Analytics API" to turn on
    "Analytics API" service.
    - Click on the "API Access" tab
    - Click the "Create an OAuth 2.0 client ID" button
    - Add a "Product name" and "Home Page URL" and then click next
    - Select "Installed application"
    - Click "Create client ID"
    3. Download client_secrets.json and put it in the same directroy as this script
  9. epicserve revised this gist Sep 5, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion list_ga_top_pages.py
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@
    pip install google-api-python-client==1.2
    2. Go to http://? to get your API credentials
    2. Go to https://code.google.com/apis/console/ to get your API credentials
    3. Download client_secrets.json and put it in the same directroy as this script
  10. epicserve revised this gist Sep 5, 2013. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions list_ga_top_pages.py
    Original file line number Diff line number Diff line change
    @@ -41,6 +41,8 @@
    import httplib2
    import os

    __author__ = "epicserve@gmail.com (Brent O'Connor)"


    def get_service(argv, name, version, doc, filename, scope=None, parents=[]):

  11. epicserve created this gist Sep 5, 2013.
    114 changes: 114 additions & 0 deletions list_ga_top_pages.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,114 @@
    """
    List the top pages for a website using Google Analytics.
    Installation
    ------------
    1. Install the required python library::
    pip install google-api-python-client==1.2
    2. Go to http://? to get your API credentials
    3. Download client_secrets.json and put it in the same directroy as this script
    Usage
    -----
    Before you run the script you'll need to get the profile id for the website
    you want to get top pages for. Login to Google Analytics and then view the
    analytics for the website you want the top pages for. The property ID will be
    the number after the "p" in the URL.
    If the URL was
    https://www.google.com/analytics/web/?hl=en&pli=1#report/visitors-overview/a1212121w23232323p34343434/,
    then your profile ID would be 34343434.
    Run the script::
    python list_ga_top_pages.py --profile_id 34343434 --start_date '2013-09-01' \
    --end_date '2013-09-05' --filter '^/news/201*' --max_results 20
    """
    from apiclient import discovery
    from oauth2client import client
    from oauth2client import file
    from oauth2client import tools

    import sys
    import argparse
    import httplib2
    import os


    def get_service(argv, name, version, doc, filename, scope=None, parents=[]):

    if scope is None:
    scope = 'https://www.googleapis.com/auth/' + name

    # Parser command-line arguments.
    parent_parsers = [tools.argparser]
    parent_parsers.extend(parents)
    parser = argparse.ArgumentParser(
    description=doc,
    formatter_class=argparse.RawDescriptionHelpFormatter,
    parents=parent_parsers)
    flags = parser.parse_args(argv[1:])

    # Name of a file containing the OAuth 2.0 information for this
    # application, including client_id and client_secret, which are found
    # on the API Access tab on the Google APIs
    # Console <http://code.google.com/apis/console>.
    client_secrets = os.path.join(os.path.dirname(filename), 'client_secrets.json')

    # Set up a Flow object to be used if we need to authenticate.
    flow = client.flow_from_clientsecrets(
    client_secrets,
    scope=scope,
    message=tools.message_if_missing(client_secrets))

    # Prepare credentials, and authorize HTTP object with them.
    # If the credentials don't exist or are invalid run through the native client
    # flow. The Storage object will ensure that if successful the good
    # credentials will get written back to a file.
    storage = file.Storage(name + '.dat')
    credentials = storage.get()
    if credentials is None or credentials.invalid:
    credentials = tools.run_flow(flow, storage, flags)
    http = credentials.authorize(http=httplib2.Http())

    # Construct a service object via the discovery service.
    service = discovery.build(name, version, http=http)
    return (service, flags)


    def get_top_pages(service, profile_id, start_date, end_data, filter='^/*', max_results=50):

    return service.data().ga().get(
    ids='ga:' + str(profile_id),
    start_date=start_date,
    end_date=end_data,
    metrics='ga:pageviews',
    dimensions='ga:pagePath',
    sort='-ga:pageviews',
    filters='ga:pagePath=~' + filter,
    start_index='1',
    max_results=max_results).execute()

    if __name__ == '__main__':

    parser = argparse.ArgumentParser(description='List the top pages for a website.', add_help=False)
    parser.add_argument('-pid', '--profile_id', type=int, required=True)
    parser.add_argument('--start_date', required=True, help='2013-09-01')
    parser.add_argument('--end_date', required=True, help='2013-09-05')
    parser.add_argument('--filter', default='^/*', help='^/news/201*')
    parser.add_argument('--max_results', default=50, type=int)
    args = parser.parse_args()

    service, flags = get_service(sys.argv, 'analytics', 'v3', __doc__, __file__, scope='https://www.googleapis.com/auth/analytics.readonly', parents=[parser])
    results = get_top_pages(service, args.profile_id, start_date=args.start_date, end_data=args.end_date, filter=args.filter, max_results=args.max_results)

    if results.get('rows', []):
    for row in results.get('rows'):
    print('{0:<152}{1:>5}'.format(*row))