ในคู่มือเริ่มต้นฉบับย่อนี้ คุณจะสร้างคำขอไปยัง Google Marketing Platform Admin API และดูการตอบกลับที่มีรายชื่อบัญชี Google Analytics ที่ลิงก์กับองค์กร Google Marketing Platform ที่ระบุ
คุณสามารถทำตามคู่มือเริ่มต้นฉบับย่อนี้ให้เสร็จสมบูรณ์ได้โดยใช้ SDK ของภาษาโปรแกรมในสภาพแวดล้อมในเครื่องหรือ REST API
ข้อกำหนดเบื้องต้น
คุณต้องมีสิ่งต่อไปนี้จึงจะทำตามคู่มือเริ่มต้นฉบับย่อนี้ให้เสร็จสมบูรณ์ได้
- ตั้งค่าโปรเจ็กต์ที่อยู่ในระบบคลาวด์ของ Google และเปิดใช้ Google Marketing Platform Admin API
- ในเครื่องของคุณ ให้ทำดังนี้
- ติดตั้ง เริ่มต้น และตรวจสอบสิทธิ์ด้วย Google Cloud
- ติดตั้ง SDK สำหรับภาษาของคุณ
ตั้งค่าโปรเจ็กต์ Google Cloud
ตั้งค่าโปรเจ็กต์ที่อยู่ในระบบคลาวด์ของ Google และเปิดใช้ Google Marketing Platform Admin API
คลิกปุ่มนี้เพื่อเลือกหรือสร้างโปรเจ็กต์ที่อยู่ในระบบคลาวด์ Google ใหม่และเปิดใช้ Google Marketing Platform Admin API โดยอัตโนมัติ
เปิดใช้ Google Marketing Platform Admin APIตั้งค่า Google Cloud
ตั้งค่าและตรวจสอบสิทธิ์ด้วย Google Cloud ในเครื่องของคุณ
-
ติดตั้งและเริ่มต้น Google Cloud
-
หากคุณติดตั้ง Google Cloud ไว้ก่อนหน้านี้ ให้ตรวจสอบว่าคอมโพเนนต์
gcloudอัปเดตแล้วโดยเรียกใช้คำสั่งนี้gcloud components update
-
หากต้องการตรวจสอบสิทธิ์ด้วย Google Cloud ให้สร้างไฟล์ข้อมูลรับรองเริ่มต้นของแอปพลิเคชัน (ADC) ในเครื่อง โดยเรียกใช้คำสั่งนี้ ระบบจะใช้โฟลว์เว็บ ที่คำสั่งเปิดตัวเพื่อระบุข้อมูลเข้าสู่ระบบของผู้ใช้
gcloud auth application-default login --scopes="https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/marketingplatformadmin.analytics.read"
โปรดทราบว่าขอบเขตที่ Google Marketing Platform Admin API กำหนดไว้จะระบุไว้ในคำสั่งอย่างไร
ดูข้อมูลเพิ่มเติมได้ที่ตั้งค่าข้อมูลรับรองเริ่มต้นของแอปพลิเคชัน
Google Marketing Platform Admin API กำหนดให้มีโปรเจ็กต์โควต้า ซึ่งไม่ได้ตั้งค่าไว้โดยค่าเริ่มต้น ดูวิธีตั้งค่าโปรเจ็กต์โควต้าได้ที่ คู่มือข้อมูลเข้าสู่ระบบของผู้ใช้
ระบุรหัสองค์กร Google Marketing Platform
หากต้องการใช้ Google Marketing Platform Admin API คุณต้องระบุรหัสองค์กร Google Marketing Platform เข้าสู่ระบบ Google Marketing Platform, ไปที่กล่องโต้ตอบ การดูแลระบบ เลือกองค์กร แล้วจด รหัสองค์กร ในส่วน รายละเอียดองค์กร
คำขอทั้งหมดไปยัง Google Marketing Platform Admin API ต้องมีรหัสองค์กรในรูปแบบ organizations/ORGANIZATION_ID
ตั้งค่า SDK สำหรับภาษาโปรแกรม
ในเครื่องของคุณ ให้คลิกแท็บใดแท็บหนึ่งต่อไปนี้เพื่อติดตั้ง SDK สำหรับภาษาโปรแกรม
Java
PHP
Python
Node.js
.NET
Ruby
REST
กำหนดค่าตัวแปรสภาพแวดล้อมโดยป้อนข้อมูลต่อไปนี้
- แทนที่
ORGANIZATION_IDด้วยรหัสองค์กร Google Marketing Platform - แทนที่
PROJECT_IDด้วยรหัสโปรเจ็กต์ที่อยู่ในระบบคลาวด์ของ Google
เรียก API
ตอนนี้คุณสามารถใช้ Google Marketing Platform API เพื่อแสดงรายการบัญชี Google Analytics ที่ลิงก์กับองค์กร Google Marketing Platform ที่ระบุ เรียกใช้โค้ดต่อไปนี้เพื่อทำการเรียก API ครั้งแรก
Java
นำการเรียก .setPageSize และ .setPageToken ออกเมื่อเรียกใช้คู่มือเริ่มต้นฉบับย่อ
import com.google.ads.marketingplatform.admin.v1alpha.AnalyticsAccountLink; import com.google.ads.marketingplatform.admin.v1alpha.ListAnalyticsAccountLinksRequest; import com.google.ads.marketingplatform.admin.v1alpha.MarketingplatformAdminServiceClient; import com.google.ads.marketingplatform.admin.v1alpha.OrganizationName; public class SyncListAnalyticsAccountLinks { public static void main(String[] args) throws Exception { syncListAnalyticsAccountLinks(); } public static void syncListAnalyticsAccountLinks() throws Exception { // This snippet has been automatically generated and should be regarded as a code template only. // It will require modifications to work: // - It may require correct/in-range values for request initialization. // - It may require specifying regional endpoints when creating the service client as shown in // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library try (MarketingplatformAdminServiceClient marketingplatformAdminServiceClient = MarketingplatformAdminServiceClient.create()) { ListAnalyticsAccountLinksRequest request = ListAnalyticsAccountLinksRequest.newBuilder() .setParent(OrganizationName.of("[ORGANIZATION]").toString()) .setPageSize(883849137) .setPageToken("pageToken873572522") .build(); for (AnalyticsAccountLink element : marketingplatformAdminServiceClient.listAnalyticsAccountLinks(request).iterateAll()) { // doThingsWith(element); } } } }
PHP
use Google\Ads\MarketingPlatform\Admin\V1alpha\AnalyticsAccountLink; use Google\Ads\MarketingPlatform\Admin\V1alpha\Client\MarketingplatformAdminServiceClient; use Google\Ads\MarketingPlatform\Admin\V1alpha\ListAnalyticsAccountLinksRequest; use Google\ApiCore\ApiException; use Google\ApiCore\PagedListResponse; /** * Lists the Google Analytics accounts link to the specified Google Marketing * Platform organization. * * @param string $formattedParent The parent organization, which owns this collection of Analytics * account links. Format: organizations/{org_id} * Please see {@see MarketingplatformAdminServiceClient::organizationName()} for help formatting this field. */ function list_analytics_account_links_sample(string $formattedParent): void { // Create a client. $marketingplatformAdminServiceClient = new MarketingplatformAdminServiceClient(); // Prepare the request message. $request = (new ListAnalyticsAccountLinksRequest()) ->setParent($formattedParent); // Call the API and handle any network failures. try { /** @var PagedListResponse $response */ $response = $marketingplatformAdminServiceClient->listAnalyticsAccountLinks($request); /** @var AnalyticsAccountLink $element */ foreach ($response as $element) { printf('Element data: %s' . PHP_EOL, $element->serializeToJsonString()); } } catch (ApiException $ex) { printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage()); } } /** * Helper to execute the sample. * * This sample has been automatically generated and should be regarded as a code * template only. It will require modifications to work: * - It may require correct/in-range values for request initialization. * - It may require specifying regional endpoints when creating the service client, * please see the apiEndpoint client configuration option for more details. */ function callSample(): void { $formattedParent = MarketingplatformAdminServiceClient::organizationName('[ORGANIZATION]'); list_analytics_account_links_sample($formattedParent); }
Python
# This snippet has been automatically generated and should be regarded as a # code template only. # It will require modifications to work: # - It may require correct/in-range values for request initialization. # - It may require specifying regional endpoints when creating the service # client as shown in: # https://googleapis.dev/python/google-api-core/latest/client_options.html from google.ads import marketingplatform_admin_v1alpha def sample_list_analytics_account_links(): # Create a client client = marketingplatform_admin_v1alpha.MarketingplatformAdminServiceClient() # Initialize request argument(s) request = marketingplatform_admin_v1alpha.ListAnalyticsAccountLinksRequest( parent="parent_value", ) # Make the request page_result = client.list_analytics_account_links(request=request) # Handle the response for response in page_result: print(response)
Node.js
Usage: node packages/google-marketingplatform-admin/samples/quickstart.js organizations/ORGANIZATION_ID.
/** * This snippet has been automatically generated and should be regarded as a code template only. * It will require modifications to work. * It may require correct/in-range values for request initialization. * TODO(developer): Uncomment these variables before running the sample. */ /** * Required. The parent organization, which owns this collection of Analytics * account links. Format: organizations/{org_id} */ // const parent = 'abc123' /** * Optional. The maximum number of Analytics account links to return in one * call. The service may return fewer than this value. * If unspecified, at most 50 Analytics account links will be returned. The * maximum value is 1000; values above 1000 will be coerced to 1000. */ // const pageSize = 1234 /** * Optional. A page token, received from a previous ListAnalyticsAccountLinks * call. Provide this to retrieve the subsequent page. * When paginating, all other parameters provided to * `ListAnalyticsAccountLinks` must match the call that provided the page * token. */ // const pageToken = 'abc123' // Imports the Admin library const {MarketingplatformAdminServiceClient} = require('@google-ads/marketing-platform-admin').v1alpha; // Instantiates a client const adminClient = new MarketingplatformAdminServiceClient({fallback: true}); async function callListAnalyticsAccountLinks() { // Construct request const request = { parent, }; // Run request const iterable = adminClient.listAnalyticsAccountLinksAsync(request); for await (const response of iterable) { console.log(response); } } callListAnalyticsAccountLinks();
.NET
using Google.Ads.MarketingPlatform.Admin.V1Alpha; using Google.Api.Gax; using System; public sealed partial class GeneratedMarketingplatformAdminServiceClientSnippets { /// <summary>Snippet for ListAnalyticsAccountLinks</summary> /// <remarks> /// This snippet has been automatically generated and should be regarded as a code template only. /// It will require modifications to work: /// - It may require correct/in-range values for request initialization. /// - It may require specifying regional endpoints when creating the service client as shown in /// https://cloud.google.com/dotnet/docs/reference/help/client-configuration#endpoint. /// </remarks> public void ListAnalyticsAccountLinks() { // Create client MarketingplatformAdminServiceClient marketingplatformAdminServiceClient = MarketingplatformAdminServiceClient.Create(); // Initialize request argument(s) string parent = "organizations/[ORGANIZATION]"; // Make the request PagedEnumerable<ListAnalyticsAccountLinksResponse, AnalyticsAccountLink> response = marketingplatformAdminServiceClient.ListAnalyticsAccountLinks(parent); // Iterate over all response items, lazily performing RPCs as required foreach (AnalyticsAccountLink item in response) { // Do something with each item Console.WriteLine(item); } // Or iterate over pages (of server-defined size), performing one RPC per page foreach (ListAnalyticsAccountLinksResponse page in response.AsRawResponses()) { // Do something with each page of items Console.WriteLine("A page of results:"); foreach (AnalyticsAccountLink item in page) { // Do something with each item Console.WriteLine(item); } } // Or retrieve a single page of known size (unless it's the final page), performing as many RPCs as required int pageSize = 10; Page<AnalyticsAccountLink> singlePage = response.ReadPage(pageSize); // Do something with the page of items Console.WriteLine($"A page of {pageSize} results (unless it's the final page):"); foreach (AnalyticsAccountLink item in singlePage) { // Do something with each item Console.WriteLine(item); } // Store the pageToken, for when the next page is required. string nextPageToken = singlePage.NextPageToken; } }
REST
หากต้องการส่งคำขอนี้ ให้เรียกใช้คำสั่ง curl จากบรรทัดคำสั่งหรือ ใส่การเรียก REST ในแอปพลิเคชัน
curl -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \\ -H "x-goog-user-project: ${PROJECT_ID}" -H "Content-Type: application/json" \\ https://marketingplatformadmin.googleapis.com/v1alpha/organizations/${ORGANIZATION_ID}/analyticsAccountLinks
โค้ดตัวอย่างจะพิมพ์การตอบกลับพร้อมรายชื่อบัญชี Google Analytics ที่ลิงก์กับองค์กร Google Marketing Platform ที่ระบุ
{
"analyticsAccountLinks": [
{
"name": "organizations/0a123456789-wxyz/analyticsAccountLinks/1234567890",
"analyticsAccount": "analyticsadmin.googleapis.com/accounts/1234567890",
"displayName": "Demo Account",
"linkVerificationState": "LINK_VERIFICATION_STATE_VERIFIED"
}
]
}
ยินดีด้วย คุณได้ส่งคำขอแรกไปยัง Google Marketing Platform API แล้ว