普段使っている MAC に、Apache Superset (incubating) — Apache Superset documentationインストールしていて、これに Google Analytics のデータを取り込んで何かしたいと考えております。
Superset には、Google Analytics のデータを直接取り込むアダプタは今のところ存在せず、Google Analytics から RDB へデータを Export する必要があります。 この、Google Analytics のデータを RDB に登録する というのが、若干面倒なので、Pandas を使って、Google スプレッドシートのデータをsqlite3 にimportする | Monotalk で、Google Spread Sheet 経由で取り込んでいました。

取り込んでみるになり、あらためて調べると、 panalysis/Google2Pandas: Query Google Analytics and get the results as a pandas.DataFrameいう 直接 Google Analytics のデータを Dataframe に変換できるライブラリがありました。
インストール から、sqlite へのデータ登録まで実施してみましたので、実施したことを記載します。

[TOC]


インストール

!python3 -m pip install Google2Pandas

    Successfully built Google2Pandas
    [31mmezzanine 4.2.3 has requirement django<1.11,>=1.8, but you'll have django 1.11.11 which is incompatible.[0m
    [31mdf2gspread 0.2.5 has requirement google-api-python-client==1.6.2, but you'll have google-api-python-client 1.6.4 which is incompatible.[0m
    [31mpywebpush 1.5.0 has requirement requests>=2.13.0, but you'll have requests 2.12.1 which is incompatible.[0m
    Installing collected packages: Google2Pandas
    Successfully installed Google2Pandas-0.1.1


Google Analytics API の設定を行う

試したコードは、サービスアカウント認証で、Google Analytics API を使用しており、キーの発行が必要になります。
キーの発行方法はいかが参考になるかと思います。
最新!】Google Analytics APIの使い方! | HPcode


Query を 実行して、結果を Dataframe 化する

panalysis/Google2Pandas: Query Google Analytics and get the results as a pandas.DataFrame記載の、Quick Demo実行してみます。

Quick Demo を実行

from google2pandas import *
view_id = 'your_view_id'
query = {
    'reportRequests': [{
        'viewId' : view_id,

        'dateRanges': [{
            'startDate' : '8daysAgo',
            'endDate'   : 'today'}],

        'dimensions' : [
            {'name' : 'ga:date'}, 
            {'name' : 'ga:pagePath'},
            {'name' : 'ga:browser'}],

        'metrics'   : [
            {'expression' : 'ga:pageviews'}],
    }]
}
# Assume we have placed our client_secrets_v4.json file in the current
# working directory.
conn = GoogleAnalyticsQueryV4(secrets='ga_client.json')
df = conn.execute_query(query)
# --------------------------------------------------------
# pageviewsで降順ソート、上位20件を抽出
# --------------------------------------------------
df.sort_values(by=["pageviews"], ascending=[False]).head(20)

browserdatepagePathpageviews
1169Chrome20180510/blog/python-folinum-を使い都道府県の夫婦年齢差をプロットする/9
1030Chrome20180510/blog/django-rest-framework-でserializer-をネストした...9
1425Chrome20180511/blog/jpa-query-selecting-only-specific-columns/9

続いて、アフィニティカテゴリ を取得してみます。

アフィニティカテゴリ を取得

from google2pandas import *

view_id = 'your_view_id'
query = {
    'reportRequests': [{
        'viewId' : view_id,
        'dateRanges': [{
            'startDate' : '8daysAgo',
            'endDate'   : 'today'}],

        'dimensions' : [
            {'name' : 'ga:userAgeBracket'}, 
            {'name' : 'ga:userGender'},
            {'name' : 'ga:interestOtherCategory'},
        ],
        'metrics'   : [
            {'expression' : 'ga:percentNewSessions'}],
    }]
}
# Assume we have placed our client_secrets_v4.json file in the current
# working directory.

conn = GoogleAnalyticsQueryV4(secrets='ga_client.json')
df = conn.execute_query(query)
# --------------------------------------------------------
# pageviewsで降順ソート、上位20件を抽出
# --------------------------------------------------
df.sort_values(by=["percentNewSessions"], ascending=[True]).head(20)

interestOtherCategorypercentNewSessionsuserAgeBracketuserGender
48Reference/Language Resources/Foreign Language ...100.018-24male
17Computers & Electronics/Software/Internet Soft...100.018-24male
218Computers & Electronics/Software/Multimedia So...100.035-44male


dataframe を sqlite に登録する

取得結果のdataframe を sqlite に登録します。 登録にはdataframe の to_sql使います。

to_sql結果をDBに登録する

from google2pandas import *

view_id = 'your_view_id'
query = {
    'reportRequests': [{
        'viewId' : view_id,

        'dateRanges': [{
            'startDate' : '8daysAgo',
            'endDate'   : 'today'}],

        'dimensions' : [
            {'name' : 'ga:date'}, 
            {'name' : 'ga:pagePath'},
            {'name' : 'ga:browser'}],

        'metrics'   : [
            {'expression' : 'ga:pageviews'}],
    }]
}
# Assume we have placed our client_secrets_v4.json file in the current
# working directory.

conn = GoogleAnalyticsQueryV4(secrets='ga_client.json')
df = conn.execute_query(query)

import sqlite3
#db作成
dbname = "sheet.db"
#dbコネクト
conn = sqlite3.connect(dbname)
# tableを作成する
df.to_sql("ga_report",  conn, index=False, if_exists="replace")
# コネクションを閉じる
conn.close()

結果の確認

# 結果の確認
dbname = "sheet.db"
#dbコネクト
conn = sqlite3.connect(dbname)
c = conn.cursor()
sql = 'select * from ga_report'
for row in c.execute(sql):
    print(row)
conn.close()

('Chrome', '20180507', '/', '2')
('Firefox', '20180507', '/', '1')
('Internet Explorer', '20180507', '/', '2')
('Chrome', '20180507', '/?page=1', '2')
('Firefox', '20180507', '/about/', '1')
('Safari', '20180507', '/amp/blog/centos-74-apache-python-連携時につまづいたところ/', '1')
('Chrome', '20180507', '/amp/blog/django-18からsettingspyのtemplatesが非推奨になって警告が出力される/', '1')
('Safari', '20180507', '/amp/blog/django-dumpdata-loaddata-でデータを移行する/', '1')
('Chrome', '20180507', '/amp/blog/Error-Cannot-find-module-core-util-is/', '1')
('Safari', '20180507', '/amp/blog/google-amp-cache-api-を使ってみる/', '1')
('Safari', '20180507', '/amp/blog/google-app-script-の-urlfetchapp-の-例外ハンドリングについて/', '2')
('Safari', '20180507', '/amp/blog/google-apps-script-で電子署名をする/', '2')
('Safari', '20180507', '/amp/blog/google-search-console-の-キーワードを-python-sklearn-randomforestclassifier-でクラス分類してカテゴリ分けする/', '1')
('Safari', '20180507', '/amp/blog/java-easybatchを使ってみる1/', '1')

Google2Pandas を使って、Google Analytics のデータを dataframe にして、sqlite に登録しました。
あまり正攻法ではなさそうに思いますが、サーバ 側のRDB のデータと、Google Analytics のデータを結合する際、便利に使えるかと個人的には思います。
以上です。

コメント