API のアルファ チャネルとベータ チャネルは両方とも引き続きサポートされます。ベータ チャネルには、アルファで利用可能な機能のサブセットが含まれており、これらは安定していると考えられており、将来的に大きな変更が加えられることは予想されません。新機能は完成したらベータ版に追加されます。
このドキュメントでは、既存のコードをGoogle Analytics Management API v3からGoogle Analytics Admin API v1に移行する方法について説明し、2 つの API の主な違いの概要を説明します。
なぜ移行する必要があるのですか?
アプリケーションで Google アナリティクス 4 プロパティを作成または構成する必要がある場合は、Admin API v1 を使用する必要があります。 Management API v3 はユニバーサル アナリティクス プロパティでのみ機能し、Admin API v1 は Google アナリティクス 4 プロパティでのみ機能します。
前提条件
クイック スタート ガイドを読んで、Admin API v1 の基本をよく理解しておく必要があります。
始めましょう
まず、Google アナリティクス 4 プロパティを準備し、Admin API v1 を有効にして、プラットフォームに適した API クライアント ライブラリを設定します。
Google アナリティクス 4 プロパティを準備する
Admin API v1 をサポートするようにコードを移行する前に、 Google アナリティクス 4 プロパティを使用するようにウェブサイトを移行する必要があります。
APIを有効にする
このボタンをクリックすると、選択した Google Cloud プロジェクトで Admin API v1 が自動的に有効になります。
Google アナリティクス管理 API を有効にするクライアントライブラリを使用する
クライアントライブラリをインストールする
クライアント ライブラリを使用する場合は、プログラミング言語の Admin API v1 クライアント ライブラリをインストールする必要があります。
ジャワ
パイソン
Node.js
。ネット
PHP
行く
go get google.golang.org/genproto/googleapis/analytics/admin/v1alpha
クライアントライブラリを初期化する
Admin API v1 クライアント ライブラリは、すぐに使い始められるように設計されています。デフォルトでは、クライアント ライブラリはサービス アカウントの資格情報を自動的に検索しようとします。
サービス アカウントの資格情報を提供する簡単な方法は、 GOOGLE_APPLICATION_CREDENTIALS
環境変数を設定することです。API クライアントは、この変数の値を使用してサービス アカウント キーの JSON ファイルを検索します。
たとえば、サービス アカウントの資格情報を設定するには、次のコマンドを実行し、サービス アカウントの JSON ファイルへのパスを使用します。
export GOOGLE_APPLICATION_CREDENTIALS="[PATH]"
以下は、Admin API v1 クライアント ライブラリを初期化するために一般的に使用されるコード スニペットです。
ジャワ
try (AlphaAnalyticsAdminClient analyticsAdmin = AlphaAnalyticsAdminClient.create()) {
パイソン
client = AlphaAnalyticsAdminClient()
。ネット
AlphaAnalyticsAdminClient client = AlphaAnalyticsAdminClient.Create();
PHP
$client = new AlphaAnalyticsAdminClient();
Node.js
// Imports the Google Analytics Data API client library. const {AlphaAnalyticsAdminClient} = require('@google-analytics/admin'); // Using a default constructor instructs the client to use the credentials // specified in GOOGLE_APPLICATION_CREDENTIALS environment variable. const analyticsAdminClient = new AlphaAnalyticsAdminClient();
環境変数を使用する代わりに、初期化中に認証情報を API クライアント インスタンスに明示的に渡すこともできます。
クライアントライブラリを使用しない場合
クライアント ライブラリなしで Management API v3 を使用し、引き続き Admin API v1 を使用する場合は、資格情報を引き続き使用できます。
Admin API によって提供される新しい HTTP エンドポイントと検出ドキュメントを使用する必要があります。
コードでディスカバリ ドキュメントを利用する場合は、Admin API v1 によって提供されるディスカバリ ドキュメントにコードを更新する必要があります。
エンドポイントを更新した後、JSON クエリを更新するには、Admin API の新しいリクエスト構造と概念を理解する必要があります。
一般的なタスク
アカウントの管理
Admin API v1 は、Management API v3 と同等の Google Analytics アカウントおよびプロパティ管理メソッドのセットを提供します。さらに、Admin API v1 では、Google Analytics アカウントをプロビジョニング、削除、更新する機能が導入されています。
Admin API v1 プロパティ管理メソッドでは、Google アナリティクス 4 プロパティのみがサポートされています。
ビュー (プロファイル) の概念は、Admin API v1 には存在しません。
Google アナリティクス 4 でデータ ストリームが導入されて以来、特定の情報がプロパティ レベルで存在しなくなりました。たとえば、
websiteUrl
フィールドは、プロパティ オブジェクトの一部ではなく、DataStream
エンティティに存在するようになりました。
アカウントの概要をリストする
Admin API のaccountSummaries
メソッドは、 Management API v3 のaccountSummaries
メソッドと同様に、呼び出し元がアクセスできるすべてのアカウントの軽量の概要を返します。
重要な違いは、Admin API v1 は Google アナリティクス 4 プロパティに関する情報のみを返すのに対し、Management API v3 の応答にはユニバーサル アナリティクスで作成されたプロパティに関するデータが含まれていることです。 1 回の呼び出しで両方のタイプのプロパティに関する情報を取得することはできません。
Google アナリティクス 4 にはビュー (プロファイル) がないため、Admin API によって返されるアカウントの概要にはビュー (プロファイル) 情報が含まれません。
Google アナリティクス 4 アカウントの概要データは、現在のユーザーが利用できるアカウント/プロパティのリソースと表示名に限定されます。
account.get
およびproperty.get
メソッドを呼び出して、サマリー応答のaccount
、 property
フィールドで返されたリソース名を使用して、完全な構成データを取得します。
管理 API v1 リクエスト
GET https://analyticsadmin.googleapis.com/v1beta/accountSummaries?key=[YOUR_API_KEY]
管理 API v1 の応答
{
"accountSummaries": [
{
"name": "accountSummaries/XXXXXX",
"account": "accounts/XXXXXX",
"displayName": "Test",
"propertySummaries": [
{
"property": "properties/XXXXXX",
"displayName": "Test App"
}
]
},
...
}
管理 API v3 リクエスト
GET https://analytics.googleapis.com/analytics/v3/management/accountSummaries?key=[YOUR_API_KEY]
管理 API v3 応答
{
"kind": "analytics#accountSummaries",
"username": "XXXXXX",
"totalResults": 9,
"startIndex": 1,
"itemsPerPage": 1000,
"items": [
{
"id": "XXXXXX",
"kind": "analytics#accountSummary",
"name": "Test Account",
"webProperties": [
{
"kind": "analytics#webPropertySummary",
"id": "UA-XXXXXX",
"name": "Test Property",
"internalWebPropertyId": "XXXXXX",
"level": "STANDARD",
"websiteUrl": "XXXXXX",
"profiles": [
{
"kind": "analytics#profileSummary",
"id": "XXXXXX",
"name": "Test Profile",
"type": "WEB"
}
]
},
...
}
クライアント ライブラリを使用して Admin API v1 を呼び出すサンプル コード:
パイソン
from google.analytics.admin import AnalyticsAdminServiceClient def list_account_summaries(transport: str = None) -> None: """ Prints summaries of all accounts accessible by the caller. Args: transport(str): The transport to use. For example, "grpc" or "rest". If set to None, a transport is chosen automatically. """ client = AnalyticsAdminServiceClient(transport=transport) results = client.list_account_summaries() print("Result:") for account_summary in results: print("-- Account --") print(f"Resource name: {account_summary.name}") print(f"Account name: {account_summary.account}") print(f"Display name: {account_summary.display_name}") print() for property_summary in account_summary.property_summaries: print("-- Property --") print(f"Property resource name: {property_summary.property}") print(f"Property display name: {property_summary.display_name}") print()
アカウントをリストする
Admin API v1 のaccounts.list
メソッドは、 Management API v3 のaccounts.list
メソッドと同様に、呼び出し元がアクセスできるすべてのアカウントを返します。
管理 API v1 リクエスト
GET https://analyticsadmin.googleapis.com/v1beta/accounts?key=[YOUR_API_KEY]
管理 API v1 の応答
{
"accounts": [
{
"name": "accounts/XXXXXX",
"createTime": "2020-02-21T00:17:33.282Z",
"updateTime": "2021-01-07T02:47:57.386Z",
"displayName": "Test Account",
"regionCode": "US"
},
...
}
管理 API v3 リクエスト
GET https://analytics.googleapis.com/analytics/v3/management/accounts?key=[YOUR_API_KEY]
管理 API v3 応答
{
"kind": "analytics#accounts",
"username": "XXXXXX",
"totalResults": 9,
"startIndex": 1,
"itemsPerPage": 1000,
"items": [
{
"id": "XXXXXX",
"kind": "analytics#account",
"selfLink": "https://www.googleapis.com/analytics/v3/management/accounts/XXXXXX",
"name": "Test Account",
"permissions": {
"effective": [
"COLLABORATE",
"EDIT",
"READ_AND_ANALYZE"
]
},
"created": "2020-02-21T00:17:33.282Z",
"updated": "2021-01-07T02:47:57.386Z",
"childLink": {
"type": "analytics#webproperties",
"href": "https://www.googleapis.com/analytics/v3/management/accounts/XXXXXX/webproperties"
}
},
...
}
Admin API v1 の応答には、有効なユーザー権限に関する情報や、Management API v3 に存在していたchildLink
フィールドが含まれていないことに注意してください。
応答には、現在 Google アナリティクス 4 プロパティを持たない可能性のあるアカウントも含まれます。
クライアント ライブラリを使用して Admin API v1 を呼び出すサンプル コード:
パイソン
from google.analytics.admin import AnalyticsAdminServiceClient from accounts_get import print_account def list_accounts(transport: str = None): """ Lists the Google Analytics accounts available to the current user. Args: transport(str): The transport to use. For example, "grpc" or "rest". If set to None, a transport is chosen automatically. """ client = AnalyticsAdminServiceClient(transport=transport) results = client.list_accounts() print("Result:") for account in results: print_account(account)
プロパティの管理
リストのプロパティ
Admin API v1 のproperties.list
メソッドは、呼び出し元がアクセスできるすべての Google アナリティクス 4 プロパティを返します。 これは、アクセス可能なすべてのユニバーサル アナリティクス プロパティを返すManagement API v3 のwebproperties.list
メソッドと同様です。
Google アナリティクス 4 では、 websiteUrl
フィールドがプロパティ レベルで存在しなくなったため、 properties.dataStreams/list
使用してプロパティに関連付けられたすべてのデータ ストリームをリストし、 defaultUri
フィールドを検索してストリームに関連付けられたウェブサイト URL を取得します。
properties.list
リクエストの URL 内のfilter
パラメーターに注意してください。パラメータの値にはリクエストの結果をフィルタリングするための式が含まれており、指定された Google Analytics アカウント ID またはリンクされた Firebase プロジェクトに関連付けられたプロパティを一覧表示するために使用できます。
管理 API v1 リクエスト
GET https://analyticsadmin.googleapis.com/v1beta/properties?filter=parent:accounts/XXXXXX&key=[YOUR_API_KEY]
管理 API v1 の応答
{
"properties": [
{
"name": "properties/XXXXXX",
"parent": "accounts/XXXXXX",
"createTime": "2020-10-29T04:02:49.124Z",
"updateTime": "2020-10-29T04:02:49.124Z",
"displayName": "Test Property",
"timeZone": "America/Los_Angeles",
"currencyCode": "USD"
},
...
}
管理 API v3 リクエスト
GET https://analytics.googleapis.com/analytics/v3/management/accounts/XXXXXX/webproperties?key=[YOUR_API_KEY]
管理 API v3 応答
{
"kind": "analytics#webproperties",
"username": "XXXXXX",
"totalResults": 33,
"startIndex": 1,
"itemsPerPage": 1000,
"items": [
{
"id": "UA-XXXXXX-1",
"kind": "analytics#webproperty",
"selfLink": "https://www.googleapis.com/analytics/v3/management/accounts/XXXXXX/webproperties/UA-XXXXXX-1",
"accountId": "XXXXXX",
"internalWebPropertyId": "XXXXXX",
"name": "Test Account",
"websiteUrl": "XXXXXX",
"level": "PREMIUM",
"profileCount": 4,
"industryVertical": "HEALTH",
"defaultProfileId": "XXXXXX",
"dataRetentionTtl": "INDEFINITE",
"dataRetentionResetOnNewActivity": true,
"permissions": {
"effective": [
"COLLABORATE",
"EDIT",
"READ_AND_ANALYZE"
]
},
"created": "2020-02-21T00:28:47.287Z",
"updated": "2021-01-27T21:39:22.704Z",
"parentLink": {
"type": "analytics#account",
"href": "https://www.googleapis.com/analytics/v3/management/accounts/XXXXXX"
},
"childLink": {
"type": "analytics#profiles",
"href": "https://www.googleapis.com/analytics/v3/management/accounts/XXXXXX/webproperties/UA-XXXXXX-1/profiles"
}
},
...
}
クライアント ライブラリを使用して Admin API v1 を呼び出すサンプル コード:
パイソン
from google.analytics.admin import AnalyticsAdminServiceClient from google.analytics.admin_v1alpha.types import ListPropertiesRequest def run_sample(): """Runs the sample.""" # TODO(developer): Replace this variable with your Google Analytics # account ID (e.g. "123456") before running the sample. account_id = "YOUR-GA-ACCOUNT-ID" list_properties(account_id) def list_properties(account_id: str, transport: str = None): """ Lists Google Analytics 4 properties under the specified parent account that are available to the current user. Args: account_id(str): The Google Analytics account ID. transport(str): The transport to use. For example, "grpc" or "rest". If set to None, a transport is chosen automatically. """ client = AnalyticsAdminServiceClient(transport=transport) results = client.list_properties( ListPropertiesRequest(filter=f"parent:accounts/{account_id}", show_deleted=True) ) print("Result:") for property_ in results: print(property_) print()
プロパティの取得
Admin API v1 のproperties.get
メソッドは、 Management API v3 のwebproperties.get
メソッドと同様に、Google アナリティクス 4 プロパティに関する情報を返します。
Admin API のproperties.get
メソッドは、Google アナリティクス 4 プロパティのみをサポートしていることに注意してください。
管理 API v1 リクエスト
GET https://analyticsadmin.googleapis.com/v1beta/properties/XXXXXX?key=[YOUR_API_KEY]
管理 API v1 の応答
{
"name": "properties/XXXXXX",
"parent": "accounts/XXXXXX",
"createTime": "2021-04-30T21:32:49.804Z",
"updateTime": "2021-04-30T21:32:49.804Z",
"displayName": "Test Property",
"industryCategory": "FINANCE",
"timeZone": "America/Los_Angeles",
"currencyCode": "USD"
}
管理 API v3 リクエスト
GET https://analytics.googleapis.com/analytics/v3/management/accounts/XXXXXX/webproperties/UA-XXXXXX-3?key=[YOUR_API_KEY]
管理 API v3 応答
{
"id": "UA-XXXXXX-3",
"kind": "analytics#webproperty",
"selfLink": "https://www.googleapis.com/analytics/v3/management/accounts/XXXXXX/webproperties/UA-XXXXXX-3",
"accountId": "XXXXXX",
"internalWebPropertyId": "XXXXXX",
"name": "Test 2",
"websiteUrl": "YOUR-WEBSITE-URL",
"level": "STANDARD",
"profileCount": 0,
"industryVertical": "FINANCE",
"dataRetentionTtl": "MONTHS_26",
"dataRetentionResetOnNewActivity": true,
"permissions": {
"effective": [
"COLLABORATE",
"EDIT",
"MANAGE_USERS",
"READ_AND_ANALYZE"
]
},
"created": "2021-05-20T05:35:51.985Z",
"updated": "2021-05-20T05:41:39.219Z",
"parentLink": {
"type": "analytics#account",
"href": "https://www.googleapis.com/analytics/v3/management/accounts/XXXXXX"
},
"childLink": {
"type": "analytics#profiles",
"href": "https://www.googleapis.com/analytics/v3/management/accounts/XXXXXX/webproperties/UA-XXXXXX-3/profiles"
}
}
クライアント ライブラリを使用して Admin API v1 を呼び出すサンプル コード:
パイソン
from google.analytics.admin import AnalyticsAdminServiceClient from google.analytics.admin_v1alpha.types import IndustryCategory def run_sample(): """Runs the sample.""" # TODO(developer): Replace this variable with your Google Analytics 4 # property ID (e.g. "123456") before running the sample. property_id = "YOUR-GA4-PROPERTY-ID" get_property(property_id) def get_property(property_id: str, transport: str = None): """ Retrieves the Google Analytics 4 property details. Args: property_id(str): The Google Analytics Property ID. transport(str): The transport to use. For example, "grpc" or "rest". If set to None, a transport is chosen automatically. """ client = AnalyticsAdminServiceClient(transport=transport) property_ = client.get_property(name=f"properties/{property_id}") print("Result:") print_property(property_) def print_property(property): """Prints the Google Analytics 4 property details.""" print(f"Resource name: {property.name}") print(f"Parent: {property.parent}") print(f"Display name: {property.display_name}") print(f"Create time: {property.create_time}") print(f"Update time: {property.update_time}") # print(f"Delete time: {property.delete_time}") # print(f"Expire time: {property.expire_time}") if property.industry_category: print(f"Industry category: {IndustryCategory(property.industry_category).name}") print(f"Time zone: {property.time_zone}") print(f"Currency code: {property.currency_code}")
プロパティの作成
Admin API v1 のproperties.create
メソッドは、 Management API v3 のwebproperties.insert
メソッドと同様に、新しい Google アナリティクス 4 プロパティを作成します。
管理 API v1 リクエスト
POST https://analyticsadmin.googleapis.com/v1beta/properties?key=[YOUR_API_KEY]
{
"displayName": "Test Property",
"industryCategory": "AUTOMOTIVE",
"currencyCode": "USD",
"timeZone": "America/Los_Angeles",
"parent": "accounts/XXXXXX"
}
管理 API v1 の応答
{
"name": "properties/XXXXXX",
"parent": "accounts/XXXXXX",
"createTime": "2021-05-20T09:16:08.458Z",
"updateTime": "2021-05-20T09:16:08.458Z",
"displayName": "Test Property",
"industryCategory": "AUTOMOTIVE",
"timeZone": "America/Los_Angeles",
"currencyCode": "USD"
}
管理 API v3 リクエスト
POST https://analytics.googleapis.com/analytics/v3/management/accounts/XXXXXX/webproperties?key=[YOUR_API_KEY]
{
"name": "Test",
"websiteUrl": "YOUR-WEBSITE-URL"
}
管理 API v3 応答
{
"id": "UA-XXXXXX-3",
"kind": "analytics#webproperty",
"selfLink": "https://www.googleapis.com/analytics/v3/management/accounts/XXXXXX/webproperties/UA-XXXXXX-3",
"accountId": "XXXXXX",
"internalWebPropertyId": "XXXXXX",
"name": "Test",
"websiteUrl": "YOUR-WEBSITE-URL",
"level": "STANDARD",
"profileCount": 0,
"dataRetentionTtl": "MONTHS_26",
"dataRetentionResetOnNewActivity": true,
"permissions": {
"effective": [
"COLLABORATE",
"EDIT",
"MANAGE_USERS",
"READ_AND_ANALYZE"
]
},
"created": "2021-05-20T05:35:51.985Z",
"updated": "2021-05-20T05:35:51.985Z",
"parentLink": {
"type": "analytics#account",
"href": "https://www.googleapis.com/analytics/v3/management/accounts/XXXXXX"
},
"childLink": {
"type": "analytics#profiles",
"href": "https://www.googleapis.com/analytics/v3/management/accounts/XXXXXX/webproperties/UA-XXXXXX-3/profiles"
}
}
クライアント ライブラリを使用して Admin API v1 を呼び出すサンプル コード:
パイソン
from google.analytics.admin import AnalyticsAdminServiceClient from google.analytics.admin_v1alpha.types import Property def run_sample(): """Runs the sample.""" # !!! ATTENTION !!! # Running this sample may change/delete your Google Analytics account # configuration. Make sure to not use the Google Analytics account ID from # your production environment below. # TODO(developer): Replace this variable with your Google Analytics # account ID (e.g. "123456") before running the sample. account_id = "YOUR-GA-ACCOUNT-ID" create_property(account_id) def create_property(account_id: str, transport: str = None): """ Creates a Google Analytics 4 property. Args: account_id(str): The Google Analytics Account ID. transport(str): The transport to use. For example, "grpc" or "rest". If set to None, a transport is chosen automatically. """ client = AnalyticsAdminServiceClient(transport=transport) property_ = client.create_property( property=Property( parent=f"accounts/{account_id}", currency_code="USD", display_name="Test property", industry_category="OTHER", time_zone="America/Los_Angeles", ) ) print("Result:") print(property_)
プロパティの更新/パッチ適用
Admin API v1 のproperties.patch
メソッドは、 Management API v3 のwebproperties.patch
メソッドと同様に、Google アナリティクス 4 プロパティの構成を更新します。
リクエストの URL 内のupdateMask
パラメータに注目してください。これには、更新するフィールドのカンマ区切りリストが含まれています。このリストに存在しないフィールドは更新されません。クライアント ライブラリを使用する場合、 update_mask
パラメータはメソッド シグネチャの一部として使用できます。
管理 API v1 リクエスト
PATCH https://analyticsadmin.googleapis.com/v1beta/properties/XXXXXX?updateMask=displayName,industryCategory&key=[YOUR_API_KEY]
{
"displayName": "New Property Name",
"industryCategory": "FINANCE"
}
管理 API v1 の応答
{
"name": "properties/XXXXXX",
"parent": "accounts/XXXXXX",
"createTime": "2021-04-30T21:32:49.804Z",
"updateTime": "2021-05-20T09:25:14.810Z",
"displayName": "New Property Name",
"industryCategory": "FINANCE",
"timeZone": "America/Los_Angeles",
"currencyCode": "USD"
}
管理 API v3 リクエスト
PATCH https://analytics.googleapis.com/analytics/v3/management/accounts/XXXXXX/webproperties/UA-XXXXXX-3?key=[YOUR_API_KEY]
{
"name": "New Property Name",
"industryVertical": "FINANCE"
}
管理 API v3 応答
{
"id": "UA-XXXXXX-3",
"kind": "analytics#webproperty",
"selfLink": "https://www.googleapis.com/analytics/v3/management/accounts/XXXXXX/webproperties/UA-XXXXXX-3",
"accountId": "XXXXXX",
"internalWebPropertyId": "XXXXXX",
"name": "New Property Name",
"websiteUrl": "XXXXXX",
"level": "STANDARD",
"profileCount": 0,
"industryVertical": "FINANCE",
"dataRetentionTtl": "MONTHS_26",
"dataRetentionResetOnNewActivity": true,
"permissions": {
"effective": [
"COLLABORATE",
"EDIT",
"MANAGE_USERS",
"READ_AND_ANALYZE"
]
},
"created": "2021-05-20T05:35:51.985Z",
"updated": "2021-05-20T05:41:39.219Z",
"parentLink": {
"type": "analytics#account",
"href": "https://www.googleapis.com/analytics/v3/management/accounts/XXXXXX"
},
"childLink": {
"type": "analytics#profiles",
"href": "https://www.googleapis.com/analytics/v3/management/accounts/XXXXXX/webproperties/UA-XXXXXX-3/profiles"
}
}
クライアント ライブラリを使用して Admin API v1 を呼び出すサンプル コード:
パイソン
from google.analytics.admin import AnalyticsAdminServiceClient from google.analytics.admin_v1alpha.types import Property from google.protobuf.field_mask_pb2 import FieldMask def run_sample(): """Runs the sample.""" # !!! ATTENTION !!! # Running this sample may change/delete your Google Analytics account # configuration. Make sure to not use the Google Analytics property ID from # your production environment below. # TODO(developer): Replace this variable with your Google Analytics 4 # property ID (e.g. "123456") before running the sample. property_id = "YOUR-GA4-PROPERTY-ID" update_property(property_id) def update_property(property_id: str, transport: str = None): """ Updates the Google Analytics 4 property. Args: property_id(str): The Google Analytics Property ID. transport(str): The transport to use. For example, "grpc" or "rest". If set to None, a transport is chosen automatically. """ client = AnalyticsAdminServiceClient(transport=transport) # This call updates the display name, industry category and time zone of the # property, as indicated by the value of the `update_mask` field. # The property to update is specified in the `name` field of the `Property` # instance. property_ = client.update_property( property=Property( name=f"properties/{property_id}", display_name="This is an updated test property", industry_category="GAMES", time_zone="America/New_York", ), update_mask=FieldMask(paths=["display_name", "time_zone", "industry_category"]), ) print("Result:") print(property_)
ユーザーを管理する
Google Analytics Admin API は現在、Management API v3 と同様のユーザー権限モデルを実装しています。
Management API v3 と同様に、ユーザーとアカウントまたは Google アナリティクス 4 プロパティの間にリンクを作成することでユーザー権限を管理できます。 userLink
エンティティには、ユーザー ID (電子メール アドレス) とユーザーに付与されたロールのリストが含まれています。ユーザー リンク エンティティは更新または削除できます。
以下は、Admin API v1 ロール名と Management API v3 権限名の間のマッピングです。
管理者 API v1 ロール | Management API v3 権限名 |
---|---|
事前定義された役割/ビューア | 読み取りと分析 |
事前定義された役割/アナリスト | コラボレーションする |
事前定義された役割/エディター | 編集 |
事前定義された役割/管理者 | MANAGE_USERS |
ロールpredefinedRoles/no-cost-data
、 predefinedRoles/no-revenue-data
Admin API v1 で導入され、Management API v3 には対応するマッピングがありません。
アカウント ユーザー リンクの一覧表示
Admin API v1 のaccounts.userLinks.list
メソッドは、 Management API v3 のaccountUserLinks.list
メソッドと同様に、アカウント上のすべてのユーザー リンクを一覧表示します。
管理 API v1 リクエスト
GET https://analyticsadmin.googleapis.com/v1alpha/accounts/XXXXXX/userLinks?key=[YOUR_API_KEY]
管理 API v1 の応答
{
"userLinks": [
{
"name": "accounts/XXXXXX/userLinks/XXXXXX",
"emailAddress": "XXXXXX",
"directRoles": [
"predefinedRoles/editor",
"predefinedRoles/admin"
]
}
]
}
管理 API v3 リクエスト
GET https://analytics.googleapis.com/analytics/v3/management/accounts/XXXXXX/entityUserLinks?key=[YOUR_API_KEY]
管理 API v3 応答
{
"kind": "analytics#entityUserLinks",
"totalResults": 1,
"startIndex": 1,
"itemsPerPage": 1000,
"items": [
{
"id": "XXXXXX:XXXXXX",
"kind": "analytics#entityUserLink",
"selfLink": "https://www.googleapis.com/analytics/v3/management/accounts/XXXXXX/entityUserLinks/XXXXXX:XXXXXX",
"entity": {
"accountRef": {
"id": "XXXXXX",
"kind": "analytics#accountRef",
"href": "https://www.googleapis.com/analytics/v3/management/accounts/XXXXXX",
"name": "This is a test account"
}
},
"userRef": {
"kind": "analytics#userRef",
"id": "XXXXXX",
"email": "XXXXXX"
},
"permissions": {
"effective": [
"COLLABORATE",
"EDIT",
"MANAGE_USERS",
"READ_AND_ANALYZE"
],
"local": [
"EDIT",
"MANAGE_USERS"
]
}
}
]
}
クライアント ライブラリを使用して Admin API v1 を呼び出すサンプル コード:
パイソン
from google.analytics.admin import AnalyticsAdminServiceClient def run_sample(): """Runs the sample.""" # TODO(developer): Replace this variable with your Google Analytics # account ID (e.g. "123456") before running the sample. account_id = "YOUR-GA-ACCOUNT-ID" list_account_user_links(account_id) def list_account_user_links(account_id: str, transport: str = None): """ Lists user links under the specified parent account. Args: account_id(str): The id of the account. transport(str): The transport to use. For example, "grpc" or "rest". If set to None, a transport is chosen automatically. """ client = AnalyticsAdminServiceClient(transport=transport) results = client.list_user_links(parent=f"accounts/{account_id}") print("Result:") for user_link in results: print(user_link) print()
アカウント ユーザー リンクを更新する
Admin API v1 のaccounts.userLinks.list
メソッドは、 Management API v3 のaccountUserLinks.update
メソッドと同様に、アカウントのユーザー リンクを更新します。
管理 API v1 リクエスト
PATCH https://analyticsadmin.googleapis.com/v1alpha/accounts/XXXXXX/userLinks/104236685715552897132?key=[YOUR_API_KEY]
{
"directRoles": [
"predefinedRoles/editor",
"predefinedRoles/admin"
]
}
管理 API v1 の応答
{
"name": "accounts/XXXXXX/userLinks/104236685715552897132",
"emailAddress": "USER-EMAIL",
"directRoles": [
"predefinedRoles/editor",
"predefinedRoles/admin"
]
}
管理 API v3 リクエスト
PUT https://analytics.googleapis.com/analytics/v3/management/accounts/XXXXXX/entityUserLinks/XXXXXX%3A104236685715552897132?key=[YOUR_API_KEY]
{
"entity": {
"accountRef": {
"id": "XXXXXX"
}
},
"userRef": {
"email": "XXXXXX"
},
"permissions": {
"local": [
"EDIT",
"MANAGE_USERS"
]
}
}
管理 API v3 応答
{
"id": "XXXXXX:104236685715552897132",
"kind": "analytics#entityUserLink",
"selfLink": "https://www.googleapis.com/analytics/v3/management/accounts/XXXXXX/entityUserLinks/XXXXXX:104236685715552897132",
"entity": {
"accountRef": {
"id": "XXXXXX",
"kind": "analytics#accountRef",
"href": "https://www.googleapis.com/analytics/v3/management/accounts/XXXXXX",
"name": "This is a test account"
}
},
"userRef": {
"kind": "analytics#userRef",
"id": "104236685715552897132",
"email": "XXXXXX"
},
"permissions": {
"effective": [
"COLLABORATE",
"EDIT",
"MANAGE_USERS",
"READ_AND_ANALYZE"
],
"local": [
"EDIT",
"MANAGE_USERS"
]
}
}
クライアント ライブラリを使用して Admin API v1 を呼び出すサンプル コード:
パイソン
from google.analytics.admin import AnalyticsAdminServiceClient from google.analytics.admin_v1alpha.types import UserLink def run_sample(): """Runs the sample.""" # !!! ATTENTION !!! # Running this sample may change/delete your Google Analytics account # configuration. Make sure to not use the Google Analytics property ID from # your production environment below. # TODO(developer): Replace this variable with your Google Analytics # account ID (e.g. "123456") before running the sample. account_id = "YOUR-GA-ACCOUNT-ID" # TODO(developer): Replace this variable with your Google Analytics # account user link ID (e.g. "123456") before running the sample. account_user_link_id = "YOUR-ACCOUNT-USER-LINK-ID" update_account_user_link(account_id, account_user_link_id) def update_account_user_link( account_id: str, account_user_link_id: str, transport: str = None ): """ Updates the account user link. Args: account_id(str): The Google Analytics Account ID. account_user_link_id(str): Google Analytics account user link ID. transport(str): The transport to use. For example, "grpc" or "rest". If set to None, a transport is chosen automatically. """ client = AnalyticsAdminServiceClient(transport=transport) # This call updates the email address and direct roles of the user link. # The user link to update is specified in the `name` field of the `UserLink` # instance. user_link = client.update_user_link( user_link=UserLink( name=f"accounts/{account_id}/userLinks/{account_user_link_id}", direct_roles=["predefinedRoles/collaborate"], ), ) print("Result:") print(user_link)
アカウントユーザーリンクの作成
Admin API v1 のaccounts.userLinks.create
メソッドは、 Management API v3 のaccountUserLinks.insert
メソッドと同様に、アカウントにユーザー リンクを作成します。
管理 API v1 リクエスト
POST https://analyticsadmin.googleapis.com/v1alpha/accounts/XXXXXX/userLinks?key=[YOUR_API_KEY]
{
"directRoles": [
"predefinedRoles/editor",
"predefinedRoles/admin"
],
"emailAddress": "USER-EMAIL"
}
管理 API v1 の応答
{
"name": "accounts/XXXXXX/userLinks/104236685715552897132",
"emailAddress": "USER-EMAIL",
"directRoles": [
"predefinedRoles/editor",
"predefinedRoles/admin"
]
}
管理 API v3 リクエスト
POST https://analytics.googleapis.com/analytics/v3/management/accounts/XXXXXX/entityUserLinks?key=[YOUR_API_KEY]
{
"entity": {
"accountRef": {
"id": "XXXXXX"
}
},
"userRef": {
"email": "XXXXXX"
},
"permissions": {
"local": [
"EDIT",
"MANAGE_USERS"
]
}
}
管理 API v3 応答
{
"id": "XXXXXX:114236685715552897132",
"kind": "analytics#entityUserLink",
"selfLink": "https://www.googleapis.com/analytics/v3/management/accounts/XXXXXX/entityUserLinks/XXXXXX:114236685715552897132",
"entity": {
"accountRef": {
"id": "XXXXXX",
"kind": "analytics#accountRef",
"href": "https://www.googleapis.com/analytics/v3/management/accounts/XXXXXX",
"name": "This is a test account"
}
},
"userRef": {
"kind": "analytics#userRef",
"id": "114236685715552897132",
"email": "XXXXXX"
},
"permissions": {
"effective": [
"COLLABORATE",
"EDIT",
"MANAGE_USERS",
"READ_AND_ANALYZE"
],
"local": [
"EDIT",
"MANAGE_USERS"
]
}
}
クライアント ライブラリを使用して Admin API v1 を呼び出すサンプル コード:
パイソン
from google.analytics.admin import AnalyticsAdminServiceClient from google.analytics.admin_v1alpha.types import CreateUserLinkRequest, UserLink def run_sample(): """Runs the sample.""" # !!! ATTENTION !!! # Running this sample may change/delete your Google Analytics account # configuration. Make sure to not use the Google Analytics account ID from # your production environment below. # TODO(developer): Replace this variable with your Google Analytics # account ID (e.g. "123456") before running the sample. account_id = "YOUR-GA-ACCOUNT-ID" # TODO(developer): Replace this variable with an email address of the user to # link. This user will be given access to your account after running the # sample. email_address = "TEST-EMAIL-ADDRESS" create_account_user_link(account_id, email_address) def create_account_user_link( account_id: str, email_address: str, transport: str = None ): """ Creates a user link for the account. Args: account_id(str): The Google Analytics Account ID. email_address(str): Email address of the user to link. transport(str): The transport to use. For example, "grpc" or "rest". If set to None, a transport is chosen automatically. """ client = AnalyticsAdminServiceClient(transport=transport) user_link = client.create_user_link( CreateUserLinkRequest( parent=f"accounts/{account_id}", user_link=UserLink( email_address=email_address, direct_roles=["predefinedRoles/read"] ), notify_new_user=True, ) ) print("Result:") print(user_link)
バッチ処理
Management API v3とは異なり、Admin API v1 では、マルチパート/混合コンテンツ タイプを使用した複数の Google Analytics API 呼び出しのバッチ処理はサポートされていません。
代わりに、バッチ処理が API レベルで明示的にサポートされています。 Admin API v1 の次のメソッドはバッチ機能をサポートしています。
クライアント ライブラリを使用して Admin API v1 を呼び出すサンプル コード:
パイソン
from google.analytics.admin import AnalyticsAdminServiceClient from google.analytics.admin_v1alpha.types import ( BatchCreateUserLinksRequest, CreateUserLinkRequest, UserLink, ) def run_sample(): """Runs the sample.""" # !!! ATTENTION !!! # Running this sample may change/delete your Google Analytics account # configuration. Make sure to not use the Google Analytics account ID from # your production environment below. # TODO(developer): Replace this variable with your Google Analytics # account ID (e.g. "123456") before running the sample. account_id = "YOUR-GA-ACCOUNT-ID" # TODO(developer): Replace this variable with an email address of the user to # link. This user will be given access to your account after running the # sample. email_address = "TEST-EMAIL-ADDRESS" batch_create_account_user_link(account_id, email_address) def batch_create_account_user_link( account_id: str, email_address: str, transport: str = None ): """ Creates a user link for the account using a batch call. Args: account_id(str): The Google Analytics Account ID. email_address(str): Email address of the user to link. transport(str): The transport to use. For example, "grpc" or "rest". If set to None, a transport is chosen automatically. """ client = AnalyticsAdminServiceClient(transport=transport) response = client.batch_create_user_links( BatchCreateUserLinksRequest( parent=f"accounts/{account_id}", requests=[ CreateUserLinkRequest( user_link=UserLink( email_address=email_address, direct_roles=["predefinedRoles/read"], ) ) ], notify_new_users=True, ) ) print("Result:") for user_link in response.user_links: print(user_link) print()
API クォータの変更
Admin API v1 では、Management API v3 と比べて制限の少ないクォータが導入されています。
- GCP プロジェクトのデフォルトでは、Admin API v1 へのリクエスト数は1 分あたり 600 リクエストにレート制限されています。
- 現在、 GCP プロジェクトごとの Admin API v1 呼び出し数に 1 日あたりの制限はありません。 1 日あたりの理論上の最大リクエスト数は、 1 分あたりのリクエストの割り当てによって制限されることに注意してください。
- 1 日あたりの書き込み操作の数に対する別の制限が削除されました。