Python で Google Analytics Reporting APIを使う

Adsenseの売上を取得する

from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials

SCOPES = ["https://www.googleapis.com/auth/analytics.readonly"]
KEY_FILE_LOCATION = "xxxxxxxx.json"
VIEW_ID = "999999999"

credentials = ServiceAccountCredentials.from_json_keyfile_name(
            KEY_FILE_LOCATION, SCOPES)

analytics = build("analyticsreporting", "v4", credentials=credentials)

response = (analytics.reports()
    .batchGet(
        body={
            "reportRequests": [
                {
                    "viewId": VIEW_ID,
                    "dateRanges": [{"startDate": "3daysAgo", "endDate": "today"}],
                    "dimensions": [{"name": "ga:pagePathLevel1"},],
                    "metrics": [{"expression": "ga:adsenseRevenue"}],
                }
            ]
        }
    )
    .execute()
)

report = response.get("reports")[0]

report['data']

#出力例
{'rows': [
    {'dimensions': ['/'], 'metrics': [{'values': ['7.11312']}]},
    {'dimensions': ['/news/'], 'metrics': [{'values': ['22.22']}]}]}

Dimensionで検索

{
    "reportRequests": [
        {
            "viewId": VIEW_ID,
            ...
            ...
            ...
            "dimensionFilterClauses": [{
                "filters":[{
                    "dimensionName": "ga:eventCategory",
                    "operator": "BEGINS_WITH",
                    "expressions": ["page"],
                }]
            }]

        }
    ]
}

Segmentを指定

{
    "reportRequests": [
        {
            "viewId": VIEW_ID,
            "dateRanges": [{"startDate": "1daysAgo", "endDate": "today"}],
            "dimensions": [{"name": "ga:segment"}],
            "metrics": [{"expression": "ga:users"}],
            "segments":[
                {"segmentId": "gaid::-2",}, # New Users
                {"segmentId": "gaid::-3",}  # Returning Users
            ],

        }

    ]
}

Segmentを動的に指定

{
    "reportRequests": [
        {
            "viewId": VIEW_ID,
            "dateRanges": [{"startDate": "1daysAgo",
                            "endDate": "today"}],
            "dimensions": [{"name": "ga:segment"}],
            "metrics": [{"expression": "ga:users"}],
            "segments":[
                {
                  "dynamicSegment":
                  {
                    "name": "Dynamic new user",
                    "userSegment":
                    {
                      "segmentFilters":[
                      {
                        "simpleSegment":
                        {
                          "orFiltersForSegment":
                          {
                            "segmentFilterClauses": [
                            {
                              "dimensionFilter":
                              {
                                "dimensionName":"ga:userType",
                                "operator":"EXACT",
                                "expressions":["New Visitor"]
                              }
                            }]
                          }
                        }
                      }
                    ]
                  }
                }
              }
          ]
        }
    ]
}

セグメントIDの一覧

https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtReference/management/segments/list で取得

{
  "kind": "analytics#segments",
  "totalResults": 26,
  "startIndex": 1,
  "itemsPerPage": 1000,
  "items": [
    {
      "id": "-1",
      "kind": "analytics#segment",
      "selfLink": "https://www.googleapis.com/analytics/v3/management/segments/gaid::-1",
      "segmentId": "gaid::-1",
      "name": "All Users",
      "definition": "",
      "type": "BUILT_IN"
    },
    {
      "id": "-2",
      "kind": "analytics#segment",
      "selfLink": "https://www.googleapis.com/analytics/v3/management/segments/gaid::-2",
      "segmentId": "gaid::-2",
      "name": "New Users",
      "definition": "sessions::condition::ga:userType==New Visitor",
      "type": "BUILT_IN"
    },
    {
      "id": "-3",
      "kind": "analytics#segment",
      "selfLink": "https://www.googleapis.com/analytics/v3/management/segments/gaid::-3",
      "segmentId": "gaid::-3",
      "name": "Returning Users",
      "definition": "sessions::condition::ga:userType==Returning Visitor",
      "type": "BUILT_IN"
    },
    {
      "id": "-4",
      "kind": "analytics#segment",
      "selfLink": "https://www.googleapis.com/analytics/v3/management/segments/gaid::-4",
      "segmentId": "gaid::-4",
      "name": "Paid Traffic",
      "definition": "sessions::condition::ga:medium=~^(cpc|ppc|cpa|cpm|cpv|cpp)$",
      "type": "BUILT_IN"
    },
    {
      "id": "-5",
      "kind": "analytics#segment",
      "selfLink": "https://www.googleapis.com/analytics/v3/management/segments/gaid::-5",
      "segmentId": "gaid::-5",
      "name": "Organic Traffic",
      "definition": "sessions::condition::ga:medium==organic",
      "type": "BUILT_IN"
    },
    {
      "id": "-6",
      "kind": "analytics#segment",
      "selfLink": "https://www.googleapis.com/analytics/v3/management/segments/gaid::-6",
      "segmentId": "gaid::-6",
      "name": "Search Traffic",
      "definition": "sessions::condition::ga:medium=~^(cpc|ppc|cpa|cpm|cpv|cpp|organic)$",
      "type": "BUILT_IN"
    },
    {
      "id": "-7",
      "kind": "analytics#segment",
      "selfLink": "https://www.googleapis.com/analytics/v3/management/segments/gaid::-7",
      "segmentId": "gaid::-7",
      "name": "Direct Traffic",
      "definition": "sessions::condition::ga:medium==(none)",
      "type": "BUILT_IN"
    },
    {
      "id": "-8",
      "kind": "analytics#segment",
      "selfLink": "https://www.googleapis.com/analytics/v3/management/segments/gaid::-8",
      "segmentId": "gaid::-8",
      "name": "Referral Traffic",
      "definition": "sessions::condition::ga:medium==referral",
      "type": "BUILT_IN"
    },
    {
      "id": "-9",
      "kind": "analytics#segment",
      "selfLink": "https://www.googleapis.com/analytics/v3/management/segments/gaid::-9",
      "segmentId": "gaid::-9",
      "name": "Sessions with Conversions",
      "definition": "sessions::condition::ga:goalCompletionsAll\u003e0",
      "type": "BUILT_IN"
    },
    {
      "id": "-10",
      "kind": "analytics#segment",
      "selfLink": "https://www.googleapis.com/analytics/v3/management/segments/gaid::-10",
      "segmentId": "gaid::-10",
      "name": "Sessions with Transactions",
      "definition": "sessions::condition::ga:transactions\u003e0",
      "type": "BUILT_IN"
    },
    {
      "id": "-11",
      "kind": "analytics#segment",
      "selfLink": "https://www.googleapis.com/analytics/v3/management/segments/gaid::-11",
      "segmentId": "gaid::-11",
      "name": "Mobile and Tablet Traffic",
      "definition": "sessions::condition::ga:deviceCategory==mobile,ga:deviceCategory==tablet",
      "type": "BUILT_IN"
    },
    {
      "id": "-12",
      "kind": "analytics#segment",
      "selfLink": "https://www.googleapis.com/analytics/v3/management/segments/gaid::-12",
      "segmentId": "gaid::-12",
      "name": "Non-bounce Sessions",
      "definition": "sessions::condition::ga:bounces==0",
      "type": "BUILT_IN"
    },
    {
      "id": "-13",
      "kind": "analytics#segment",
      "selfLink": "https://www.googleapis.com/analytics/v3/management/segments/gaid::-13",
      "segmentId": "gaid::-13",
      "name": "Tablet Traffic",
      "definition": "sessions::condition::ga:deviceCategory==tablet",
      "type": "BUILT_IN"
    },
    {
      "id": "-14",
      "kind": "analytics#segment",
      "selfLink": "https://www.googleapis.com/analytics/v3/management/segments/gaid::-14",
      "segmentId": "gaid::-14",
      "name": "Mobile Traffic",
      "definition": "sessions::condition::ga:deviceCategory==mobile",
      "type": "BUILT_IN"
    },
    {
      "id": "-15",
      "kind": "analytics#segment",
      "selfLink": "https://www.googleapis.com/analytics/v3/management/segments/gaid::-15",
      "segmentId": "gaid::-15",
      "name": "Tablet and Desktop Traffic",
      "definition": "sessions::condition::ga:deviceCategory==tablet,ga:deviceCategory==desktop",
      "type": "BUILT_IN"
    },
    {
      "id": "-16",
      "kind": "analytics#segment",
      "selfLink": "https://www.googleapis.com/analytics/v3/management/segments/gaid::-16",
      "segmentId": "gaid::-16",
      "name": "Android Traffic",
      "definition": "sessions::condition::ga:operatingSystem==Android",
      "type": "BUILT_IN"
    },
    {
      "id": "-17",
      "kind": "analytics#segment",
      "selfLink": "https://www.googleapis.com/analytics/v3/management/segments/gaid::-17",
      "segmentId": "gaid::-17",
      "name": "iOS Traffic",
      "definition": "sessions::condition::ga:operatingSystem=~^(iOS|iPad|iPhone|iPod)$",
      "type": "BUILT_IN"
    },
    {
      "id": "-18",
      "kind": "analytics#segment",
      "selfLink": "https://www.googleapis.com/analytics/v3/management/segments/gaid::-18",
      "segmentId": "gaid::-18",
      "name": "Other Traffic (Neither iOS nor Android)",
      "definition": "sessions::condition::ga:operatingSystem!~^(Android|iOS|iPad|iPhone|iPod)$",
      "type": "BUILT_IN"
    },
    {
      "id": "-19",
      "kind": "analytics#segment",
      "selfLink": "https://www.googleapis.com/analytics/v3/management/segments/gaid::-19",
      "segmentId": "gaid::-19",
      "name": "Bounced Sessions",
      "definition": "sessions::condition::ga:bounces\u003e0",
      "type": "BUILT_IN"
    },
    {
      "id": "-100",
      "kind": "analytics#segment",
      "selfLink": "https://www.googleapis.com/analytics/v3/management/segments/gaid::-100",
      "segmentId": "gaid::-100",
      "name": "Single Session Users",
      "definition": "users::condition::ga:sessions==1",
      "type": "BUILT_IN"
    },
    {
      "id": "-101",
      "kind": "analytics#segment",
      "selfLink": "https://www.googleapis.com/analytics/v3/management/segments/gaid::-101",
      "segmentId": "gaid::-101",
      "name": "Multi-session Users",
      "definition": "users::condition::ga:sessions\u003e1",
      "type": "BUILT_IN"
    },
    {
      "id": "-102",
      "kind": "analytics#segment",
      "selfLink": "https://www.googleapis.com/analytics/v3/management/segments/gaid::-102",
      "segmentId": "gaid::-102",
      "name": "Converters",
      "definition": "users::condition::ga:goalCompletionsAll\u003e0,ga:transactions\u003e0",
      "type": "BUILT_IN"
    },
    {
      "id": "-103",
      "kind": "analytics#segment",
      "selfLink": "https://www.googleapis.com/analytics/v3/management/segments/gaid::-103",
      "segmentId": "gaid::-103",
      "name": "Non-Converters",
      "definition": "users::condition::ga:goalCompletionsAll==0;ga:transactions==0",
      "type": "BUILT_IN"
    },
    {
      "id": "-104",
      "kind": "analytics#segment",
      "selfLink": "https://www.googleapis.com/analytics/v3/management/segments/gaid::-104",
      "segmentId": "gaid::-104",
      "name": "Made a Purchase",
      "definition": "users::condition::ga:transactions\u003e0",
      "type": "BUILT_IN"
    },
    {
      "id": "-105",
      "kind": "analytics#segment",
      "selfLink": "https://www.googleapis.com/analytics/v3/management/segments/gaid::-105",
      "segmentId": "gaid::-105",
      "name": "Performed Site Search",
      "definition": "users::sequence::ga:searchKeyword!~^$|^\\(not set\\)$",
      "type": "BUILT_IN"
    },
    {
      "id": "I2RqyKZqSVqnXBygeuaneg",
      "kind": "analytics#segment",
      "selfLink": "https://www.googleapis.com/analytics/v3/management/segments/gaid::I2RqyKZqSVqnXBygeuaneg",
      "segmentId": "gaid::I2RqyKZqSVqnXBygeuaneg",
      "name": "amazon-clicked",
      "definition": "sessions::condition::ga:eventCategory=@amazon",
      "type": "CUSTOM",
      "created": "2021-02-07T23:35:58.320Z",
      "updated": "2021-02-07T23:35:58.320Z"
    }
  ]
}
Amazon.co.jpアソシエイト: