บทแนะนํานี้จะแนะนําขั้นตอนที่จําเป็นในการเข้าถึง API การรายงานของ Analytics เวอร์ชัน 4
1. เปิดใช้ API
หากต้องการเริ่มต้นใช้งาน Analytics Reporting API v4 คุณจะต้องใช้เครื่องมือตั้งค่าก่อน ซึ่งจะแนะนำขั้นตอนการสร้างโปรเจ็กต์ในคอนโซล Google API เปิดใช้ API และสร้างข้อมูลเข้าสู่ระบบ
สร้างข้อมูลเข้าสู่ระบบ
- เปิดหน้าบัญชีบริการ เมื่อได้รับข้อความเตือน ให้เลือกโปรเจ็กต์
- คลิก สร้างบัญชีบริการ จากนั้นป้อนชื่อและคำอธิบายของบัญชีบริการ คุณจะใช้รหัสเริ่มต้นของบัญชีบริการ หรือเลือกรหัสอื่นที่ไม่ซ้ำกันก็ได้ เมื่อเสร็จแล้วให้คลิกสร้าง
- คุณข้ามส่วนสิทธิ์ของบัญชีบริการ (ไม่บังคับ) ที่ตามมาได้ คลิกต่อไป
- ในหน้าจอให้สิทธิ์ผู้ใช้เข้าถึงบัญชีบริการนี้ ให้เลื่อนลงไปที่ส่วนสร้างคีย์ คลิก สร้างคีย์
- เลือกรูปแบบของคีย์ในแผงด้านข้างที่ปรากฏขึ้น ขอแนะนำให้เลือก JSON
- คลิกสร้าง ระบบจะสร้างคู่คีย์สาธารณะ/ส่วนตัวใหม่และดาวน์โหลดลงในเครื่องของคุณ ซึ่งจะเป็นสำเนาเพียงรายการเดียวของคีย์นี้ ดูข้อมูลเกี่ยวกับวิธีจัดเก็บคีย์อย่างปลอดภัยที่การจัดการคีย์ของบัญชีบริการ
- คลิกปิดในกล่องโต้ตอบคีย์ส่วนตัวที่บันทึกไว้ในคอมพิวเตอร์ จากนั้นคลิกเสร็จเพื่อกลับไปที่ตารางของบัญชีบริการ
เพิ่มบัญชีบริการลงในบัญชี Google Analytics
บัญชีบริการที่สร้างใหม่จะมีอีเมลที่มีลักษณะคล้ายกับรายการต่อไปนี้
quickstart@PROJECT-ID.iam.gserviceaccount.com
ใช้ที่อยู่อีเมลนี้เพื่อเพิ่มผู้ใช้ไปยังข้อมูลพร็อพเพอร์ตี้ Google Analytics ที่คุณต้องการเข้าถึงผ่าน API สำหรับบทแนะนำนี้ ต้องใช้สิทธิ์อ่านและวิเคราะห์เท่านั้น
2. ติดตั้งไลบรารีของไคลเอ็นต์
คุณสามารถรับไลบรารีของไคลเอ็นต์ Google API สำหรับ PHP ได้โดยใช้ Composer:
composer require google/apiclient:^2.0
3. ตั้งค่าตัวอย่าง
คุณจะต้องสร้างไฟล์เดียวที่ชื่อ HelloAnalytics.php
ซึ่งจะมีโค้ดตัวอย่างด้านล่าง
- คัดลอกหรือดาวน์โหลดซอร์สโค้ดต่อไปนี้ลงใน
HelloAnalytics.php
- ย้าย
service-account-credentials.json
ที่ดาวน์โหลดก่อนหน้านี้ไปยังไดเรกทอรีเดียวกับโค้ดตัวอย่าง - แทนที่ค่าของ
VIEW_ID
คุณใช้โปรแกรมสำรวจบัญชีเพื่อค้นหารหัสข้อมูลพร็อพเพอร์ตี้ได้
HelloAnalytics.php
<?php // Load the Google API PHP Client Library. require_once __DIR__ . '/vendor/autoload.php'; $analytics = initializeAnalytics(); $response = getReport($analytics); printResults($response); /** * Initializes an Analytics Reporting API V4 service object. * * @return An authorized Analytics Reporting API V4 service object. */ function initializeAnalytics() { // 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_AnalyticsReporting($client); return $analytics; } /** * Queries the Analytics Reporting API V4. * * @param service An authorized Analytics Reporting API V4 service object. * @return The Analytics Reporting API V4 response. */ function getReport($analytics) { // Replace with your view ID, for example XXXX. $VIEW_ID = "<REPLACE_WITH_VIEW_ID>"; // Create the DateRange object. $dateRange = new Google_Service_AnalyticsReporting_DateRange(); $dateRange->setStartDate("7daysAgo"); $dateRange->setEndDate("today"); // Create the Metrics object. $sessions = new Google_Service_AnalyticsReporting_Metric(); $sessions->setExpression("ga:sessions"); $sessions->setAlias("sessions"); // Create the ReportRequest object. $request = new Google_Service_AnalyticsReporting_ReportRequest(); $request->setViewId($VIEW_ID); $request->setDateRanges($dateRange); $request->setMetrics(array($sessions)); $body = new Google_Service_AnalyticsReporting_GetReportsRequest(); $body->setReportRequests( array( $request) ); return $analytics->reports->batchGet( $body ); } /** * Parses and prints the Analytics Reporting API V4 response. * * @param An Analytics Reporting API V4 response. */ function printResults($reports) { for ( $reportIndex = 0; $reportIndex < count( $reports ); $reportIndex++ ) { $report = $reports[ $reportIndex ]; $header = $report->getColumnHeader(); $dimensionHeaders = $header->getDimensions(); $metricHeaders = $header->getMetricHeader()->getMetricHeaderEntries(); $rows = $report->getData()->getRows(); for ( $rowIndex = 0; $rowIndex < count($rows); $rowIndex++) { $row = $rows[ $rowIndex ]; $dimensions = $row->getDimensions(); $metrics = $row->getMetrics(); for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) { print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n"); } for ($j = 0; $j < count($metrics); $j++) { $values = $metrics[$j]->getValues(); for ($k = 0; $k < count($values); $k++) { $entry = $metricHeaders[$k]; print($entry->getName() . ": " . $values[$k] . "\n"); } } } } }
4. เรียกใช้ตัวอย่าง
เรียกใช้ตัวอย่างโดยใช้:
php HelloAnalytics.php
เมื่อคุณทำขั้นตอนเหล่านี้เสร็จแล้ว ตัวอย่างจะแสดงจำนวนเซสชันในช่วง 7 วันที่ผ่านมาสำหรับข้อมูลพร็อพเพอร์ตี้นั้นๆ