注意
本記事内で利用しているGoogleクライアントログイン(ClientLogin)による認証方式および、Google Analytics API v2.2は現在利用できません。
以下に新しい認証方式であるOAuth2.0および、Google Analytics API v3を利用したアクセス方法をまとめていますので、こちらをご覧ください。
Google Analyticsで検索エンジンからどういうキーワードでランディングしたかを分析するときには通常、レポート画面より、「集客」>「キーワード」>「オーガニック検索」と進めて表示されるキーワード vs セッション数集計リストを活用します。
もし商用大規模サイトで例えば月間のキーワード全てを取得したい場合、この画面からCSVやTSVやExcel形式にてエクスポートをすればよいわけですが、一度に表示できる件数が最大5000件ということもあり、何万・何十万キーワードにもなる場合に何度もエクスポートしなければならなく非常に不毛な作業といえます。
ここではこれをCore Reporting APIのcurl-datafeed-v2にてXMLフィードとして取得する方法を記載します。
お手軽方式(curlコマンドを用いたシェルスクリプトにて取得)
※参考文献は公式取得用シェルスクリプトの作成
dataFeed.shという名前(名前は何でもよい)で作成しています。
============================
#!/bin/bash
#
# Copyright 2009 Google Inc. All Rights Reserved
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Access the Data Feed of the GA Data Export API through cURL
USER_EMAIL="xxxx@gmail.com" #Insert your Google Account email address here
USER_PASS="xxxx" #Insert your password here
TABLE_ID="ga:99999999" #Insert your table ID here (ie ga:1234)
googleAuth="$(curl https://www.google.com/accounts/ClientLogin -s \
-d Email=$USER_EMAIL \
-d Passwd=$USER_PASS \
-d accountType=GOOGLE \
-d source=curl-dataFeed-v2 \
-d service=analytics \
| awk /Auth=.*/)"
feedUri="https://www.google.com/analytics/feeds/data\
?ids=$TABLE_ID\
&start-date=2014-09-01\
&end-date=2014-09-30\
&dimensions=ga:keyword\
&metrics=ga:visits\
&sort=-ga:visits\
&filters=ga%3Akeyword%21%3D%28not%20set%29%3Bga%3Akeyword%21%3D%28not%20provided%29%3Bga%3Akeyword%21%3D%28other%29\
&max-results=5000\
&prettyprint=true"
curl $feedUri --silent \
--header "Authorization: GoogleLogin $googleAuth" \
--header "GData-Version: 2"
============================
※
- USER_EMAIL : Google AnalyticsのGmailアカウント名
- USER_PASS : Gmailアカウントのパスワード
- TABLE_ID : 「アナリティクス設定」>「ビュー設定」と進み表示される「ビューID」を"ga:99999999"形式にて設定
ここでは、キーワードをセッション数の多い順に並べ替えて取得するので
- ディメンジョン : "ga:keyword" ・・・ キーワード
- メトリクス : "ga:visits" ・・・ セッション数(訪問数)
- ソート順 : "-ga:visits" ・・・ セッション数(訪問数)の降順
と指定し、さらにリファラがとれなかった(not provided)ものやリファラがついてなかった(not set)、それ以外の理由でとれなかった(other)というものは集計のじゃまになるのでフィルターしています。
また、集計する期間として9月分を取得するので
- start-date : 2014-09-01
- end-date : 2014-09-30
とYYYY-MM-DD形式で指定しています。
実行方法と取得結果サンプル
このシェルはcurlコマンドを実行するのでそのまま実行すると標準出力にレスポンスデータが出力されます。なのでファイルとして出力するようリダイレクトして利用します。$> ./dataFeed.sh > dataFeed.xml
こんな感じです。これの結果ファイルの中身は次のような感じになります。
============================
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dxp="http://schemas.google.com/analytics/2009" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/">
<id>https://www.googleapis.com/analytics/v2.4/data?ids=ga:99999999&dimensions=ga:keyword&metrics=ga:visits&sort=-ga:visits&filters=ga:keyword!%3D(not+set);ga:keyword!%3D(not+provided);ga:keyword!%3D(other)&start-date=2014-09-25&end-date=2014-09-25&max-results=5000</id>
<updated>2014-10-15T04:27:18.780Z</updated>
<title type="text">Google Analytics Data for View (Profile) 99999999</title>
<link rel="self" type="application/atom+xml" href="https://www.googleapis.com/analytics/v2.4/data?ids=ga:99999999&dimensions=ga:keyword&metrics=ga:visits&sort=-ga:visits&filters=ga:keyword!%3D(not+set);ga:keyword!%3D(not+provided);ga:keyword!%3D(other)&start-date=2014-09-25&end-date=2014-09-25&max-results=5000"/>
<author>
<name>Google Analytics</name>
</author>
<generator>Google Analytics</generator>
<openSearch:totalResults>366</openSearch:totalResults>
<openSearch:startIndex>1</openSearch:startIndex>
<openSearch:itemsPerPage>5000</openSearch:itemsPerPage>
<dxp:aggregates>
<dxp:metric name="ga:visits" type="integer" value="381"/>
</dxp:aggregates>
<dxp:containsSampledData>false</dxp:containsSampledData>
<dxp:dataSource>
<dxp:property name="ga:profileId" value="99999999"/>
<dxp:property name="ga:webPropertyId" value="UA-XXXXXXXX-XX"/>
<dxp:property name="ga:accountName" value="黒耳テクニカル"/>
<dxp:tableId>ga:99999999</dxp:tableId>
<dxp:tableName>blog.miminoinu.net</dxp:tableName>
</dxp:dataSource>
<dxp:endDate>2014-09-25</dxp:endDate>
<dxp:startDate>2014-09-25</dxp:startDate>
<entry>
<id>https://www.googleapis.com/analytics/v2.4/data?ids=ga:99999999&ga:keyword=%E3%83%89%E3%83%AC%E3%82%B9%E3%82%A2%E3%83%83%E3%83%97&filters=ga:keyword!%3D(not+set);ga:keyword!%3D(not+provided);ga:keyword!%3D(other)&start-date=2014-09-25&end-date=2014-09-25</id>
<updated>2014-10-15T04:27:18.780Z</updated>
<title type="text">ga:keyword=ドレスアップ</title>
<link rel="alternate" type="text/html" href="http://www.google.com/analytics"/>
<dxp:dimension name="ga:keyword" value="ドレスアップ"/>
<dxp:metric name="ga:visits" type="integer" value="3"/>
</entry>
<entry>
<id>https://www.googleapis.com/analytics/v2.4/data?ids=ga:99999999&ga:keyword=%E5%A4%96%E8%BB%8A&filters=ga:keyword!%3D(not+set);ga:keyword!%3D(not+provided);ga:keyword!%3D(other)&start-date=2014-09-25&end-date=2014-09-25</id>
<updated>2014-10-15T04:27:18.780Z</updated>
<title type="text">ga:keyword=外車</title>
<link rel="alternate" type="text/html" href="http://www.google.com/analytics"/>
<dxp:dimension name="ga:keyword" value="外車"/>
<dxp:metric name="ga:visits" type="integer" value="2"/>
</entry>
<entry>
<id>https://www.googleapis.com/analytics/v2.4/data?ids=ga:99999999&ga:keyword=%E4%BE%A1%E6%A0%BC&filters=ga:keyword!%3D(not+set);ga:keyword!%3D(not+provided);ga:keyword!%3D(other)&start-date=2014-09-25&end-date=2014-09-25</id>
<updated>2014-10-15T04:27:18.780Z</updated>
<title type="text">ga:keyword=価格</title>
<link rel="alternate" type="text/html" href="http://www.google.com/analytics"/>
<dxp:dimension name="ga:keyword" value="価格"/>
<dxp:metric name="ga:visits" type="integer" value="2"/>
</entry>
・
・
・
・
</feed>
============================
あとはこれをXMLビューアーやExcelで開くなりして キーワード(ga:keyword)と訪問数(ga:visits)をリスト化すればよいです。