บทแนะนำนี้จะอธิบายขั้นตอนที่จำเป็นในการเข้าถึงบัญชี Google Analytics, ค้นหา Analytics API, จัดการการตอบกลับของ API และแสดงผลลัพธ์ บทแนะนำนี้ใช้ Core Reporting API v3.0, Management API v3.0 และ OAuth2.0
ขั้นตอนที่ 1: เปิดใช้ Analytics API
หากต้องการเริ่มต้นใช้งาน Google Analytics API ก่อนอื่นคุณต้องใช้เครื่องมือการตั้งค่า ซึ่งจะแนะนำขั้นตอนการสร้างโปรเจ็กต์ในคอนโซล Google API เปิดใช้ API และสร้างข้อมูลเข้าสู่ระบบ
สร้างรหัสไคลเอ็นต์
- เปิดหน้าบัญชีบริการ เมื่อได้รับข้อความเตือน ให้เลือกโปรเจ็กต์
- คลิก สร้างบัญชีบริการ จากนั้นป้อนชื่อและคำอธิบายของบัญชีบริการ คุณจะใช้รหัสเริ่มต้นของบัญชีบริการ หรือเลือกรหัสอื่นที่ไม่ซ้ำกันก็ได้ เมื่อเสร็จแล้วให้คลิกสร้าง
- คุณข้ามส่วนสิทธิ์ของบัญชีบริการ (ไม่บังคับ) ที่ตามมาได้ คลิกต่อไป
- ในหน้าจอให้สิทธิ์ผู้ใช้เข้าถึงบัญชีบริการนี้ ให้เลื่อนลงไปที่ส่วนสร้างคีย์ คลิก สร้างคีย์
- เลือกรูปแบบของคีย์ในแผงด้านข้างที่ปรากฏขึ้น ขอแนะนำให้เลือก JSON
- คลิกสร้าง ระบบจะสร้างคู่คีย์สาธารณะ/ส่วนตัวใหม่และดาวน์โหลดลงในเครื่องของคุณ ซึ่งจะเป็นสำเนาเพียงรายการเดียวของคีย์นี้ ดูข้อมูลเกี่ยวกับวิธีจัดเก็บคีย์อย่างปลอดภัยที่การจัดการคีย์ของบัญชีบริการ
- คลิกปิดในกล่องโต้ตอบคีย์ส่วนตัวที่บันทึกไว้ในคอมพิวเตอร์ จากนั้นคลิกเสร็จเพื่อกลับไปที่ตารางของบัญชีบริการ
เพิ่มบัญชีบริการลงในบัญชี Google Analytics
บัญชีบริการที่สร้างใหม่จะมีอีเมล <projectId>-<uniqueId>@developer.gserviceaccount.com
ให้ใช้อีเมลนี้เพื่อเพิ่มผู้ใช้ในบัญชี Google Analytics ที่คุณต้องการเข้าถึงผ่าน API
สำหรับบทแนะนำนี้
ต้องใช้สิทธิ์อ่านและวิเคราะห์เท่านั้น
ขั้นตอนที่ 2: ติดตั้งไลบรารีของไคลเอ็นต์ Google
คุณสามารถรับ ไลบรารีของไคลเอ็นต์ Google APIs สำหรับ PHP การดาวน์โหลดรุ่นนี้หรือใช้ Composer ดังนี้
composer require google/apiclient:^2.0
ขั้นตอนที่ 3: ตั้งค่าตัวอย่าง
คุณจะต้องสร้างไฟล์เดียวที่ชื่อ HelloAnalytics.php
ซึ่งจะมีโค้ดตัวอย่างด้านล่าง
- คัดลอกหรือดาวน์โหลดซอร์สโค้ดต่อไปนี้ลงใน
HelloAnalytics.php
- ย้าย
service-account-credentials.json
ที่ดาวน์โหลดก่อนหน้านี้ภายในไดเรกทอรีเดียวกับโค้ดตัวอย่าง
HelloAnalytics.php
<?php // Load the Google API PHP Client Library. require_once __DIR__ . '/vendor/autoload.php'; $analytics = initializeAnalytics(); $profile = getFirstProfileId($analytics); $results = getResults($analytics, $profile); printResults($results); function initializeAnalytics() { // Creates and returns the Analytics Reporting service object. // Use the developers console and download your service account // credentials in JSON format. Place them in this directory or // change the key file location if necessary. $KEY_FILE_LOCATION = __DIR__ . '/service-account-credentials.json'; // Create and configure a new client object. $client = new Google_Client(); $client->setApplicationName("Hello Analytics Reporting"); $client->setAuthConfig($KEY_FILE_LOCATION); $client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']); $analytics = new Google_Service_Analytics($client); return $analytics; } function getFirstProfileId($analytics) { // Get the user's first view (profile) ID. // Get the list of accounts for the authorized user. $accounts = $analytics->management_accounts->listManagementAccounts(); if (count($accounts->getItems()) > 0) { $items = $accounts->getItems(); $firstAccountId = $items[0]->getId(); // Get the list of properties for the authorized user. $properties = $analytics->management_webproperties ->listManagementWebproperties($firstAccountId); if (count($properties->getItems()) > 0) { $items = $properties->getItems(); $firstPropertyId = $items[0]->getId(); // Get the list of views (profiles) for the authorized user. $profiles = $analytics->management_profiles ->listManagementProfiles($firstAccountId, $firstPropertyId); if (count($profiles->getItems()) > 0) { $items = $profiles->getItems(); // Return the first view (profile) ID. return $items[0]->getId(); } else { throw new Exception('No views (profiles) found for this user.'); } } else { throw new Exception('No properties found for this user.'); } } else { throw new Exception('No accounts found for this user.'); } } function getResults($analytics, $profileId) { // Calls the Core Reporting API and queries for the number of sessions // for the last seven days. return $analytics->data_ga->get( 'ga:' . $profileId, '7daysAgo', 'today', 'ga:sessions'); } function printResults($results) { // Parses the response from the Core Reporting API and prints // the profile name and total sessions. if (count($results->getRows()) > 0) { // Get the profile name. $profileName = $results->getProfileInfo()->getProfileName(); // Get the entry for the first entry in the first row. $rows = $results->getRows(); $sessions = $rows[0][0]; // Print the results. print "First view (profile) found: $profileName\n"; print "Total sessions: $sessions\n"; } else { print "No results found.\n"; } }
ขั้นตอนที่ 4: เรียกใช้ตัวอย่าง
หลังจากเปิดใช้ Analytics API แล้ว ให้ติดตั้งไลบรารีของไคลเอ็นต์ Google APIs สำหรับ PHP และตั้งค่าซอร์สโค้ดตัวอย่างซึ่งพร้อมที่จะทำงาน
เรียกใช้ตัวอย่างโดยใช้:
php HelloAnalytics.php
เมื่อทําตามขั้นตอนเหล่านี้เสร็จแล้ว ตัวอย่างจะแสดงชื่อข้อมูลพร็อพเพอร์ตี้ (โปรไฟล์) แรกของ Google Analytics ของผู้ใช้ที่ได้รับอนุญาตและจํานวนเซสชันในช่วง 7 วันที่ผ่านมา
ด้วยออบเจ็กต์บริการ Analytics ที่ได้รับอนุญาต ตอนนี้คุณจะเรียกใช้ตัวอย่างโค้ดที่อยู่ใน เอกสารอ้างอิงของ Management API ได้แล้ว ตัวอย่างเช่น คุณอาจลองเปลี่ยนโค้ดเพื่อใช้เมธอด accountSummaries.list