YouTube Reporting API - Get Bulk Data Reports
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
YouTube จะสร้างชุดรายงานรายได้จากโฆษณาที่ระบบจัดการโดยอัตโนมัติสำหรับเจ้าของเนื้อหาที่มีสิทธิ์เข้าถึงรายงานที่เกี่ยวข้องใน Creator Studio รายงานเหล่านี้ออกแบบมาเพื่อให้สิทธิ์เข้าถึงข้อมูลแบบเป็นโปรแกรม ซึ่งเป็นข้อมูลเดียวกันกับที่อยู่ในรายงานแบบดาวน์โหลดด้วยตนเองที่เข้าถึงได้ในเมนูรายงานของ YouTube Creator Studio
หมายเหตุ: API ให้สิทธิ์เข้าถึงรายงานชุดอื่นที่ไม่ใช่ Creator Studio แม้ว่ารายงานจะมีข้อมูลที่คล้ายกันก็ตาม รายงาน API อาจมีช่องที่แตกต่างกันและใช้ชื่อช่องที่แตกต่างจากรายงาน Creator Studio ด้วย
เนื่องจาก YouTube สร้างรายงานที่ระบบจัดการโดยอัตโนมัติ กระบวนการดึงข้อมูลรายงานเหล่านี้จึงแตกต่างจากรายงานข้อมูลจำนวนมากของข้อมูลวิเคราะห์ YouTube ที่พร้อมใช้งานผ่าน API
การเรียกข้อมูลรายงาน
ขั้นตอนต่อไปนี้อธิบายวิธีดึงข้อมูลรายงานที่ระบบจัดการผ่าน API
ขั้นตอนที่ 1: ดึงข้อมูลเข้าสู่ระบบการให้สิทธิ์
คำขอทั้งหมดของ YouTube Reporting API ต้องได้รับอนุญาต คู่มือการให้สิทธิ์จะอธิบายวิธีใช้โปรโตคอล OAuth 2.0 เพื่อดึงโทเค็นการให้สิทธิ์
คำขอ YouTube Reporting API ใช้ขอบเขตการให้สิทธิ์ต่อไปนี้
ขอบเขต |
https://www.googleapis.com/auth/yt-analytics.readonly |
ดูรายงานข้อมูลวิเคราะห์ YouTube สำหรับเนื้อหา YouTube ของคุณ ขอบเขตนี้ให้สิทธิ์เข้าถึงเมตริกกิจกรรมของผู้ใช้ เช่น จำนวนการดูและจำนวนการให้คะแนน |
https://www.googleapis.com/auth/yt-analytics-monetary.readonly |
ดูรายงานด้านการเงินของข้อมูลวิเคราะห์ YouTube สำหรับเนื้อหา YouTube ของคุณ ขอบเขตนี้ให้สิทธิ์เข้าถึงเมตริกกิจกรรมของผู้ใช้ รวมถึงเมตริกรายได้และประสิทธิภาพโฆษณาโดยประมาณ |
ขั้นตอนที่ 2: ดึงข้อมูลรหัสงานสำหรับรายงานที่ต้องการ
เรียกใช้เมธอด jobs.list
เพื่อดึงข้อมูลรายการงานที่ระบบจัดการ ตั้งค่าพารามิเตอร์ includeSystemManaged
เป็น true
พร็อพเพอร์ตี้ reportTypeId
ในทรัพยากร Job
ที่ส่งคืนแต่ละรายการจะระบุประเภทของรายงานที่ระบบจัดการซึ่งเชื่อมโยงกับงานนั้น แอปพลิเคชันของคุณต้องมีค่าพร็อพเพอร์ตี้ id
จากแหล่งข้อมูลเดียวกันในขั้นตอนต่อไปนี้
เอกสารรายงานจะแสดงรายงานที่มีอยู่ รหัสประเภทรายงาน และฟิลด์ที่อยู่ในรายงาน นอกจากนี้ คุณยังใช้เมธอด reportTypes.list
เพื่อดึงข้อมูลรายการประเภทรายงานที่รองรับได้ด้วย
ขั้นตอนที่ 3: ดึง URL การดาวน์โหลดของรายงาน
เรียกใช้เมธอด jobs.reports.list
เพื่อดึงข้อมูลรายการรายงานที่สร้างขึ้นสำหรับงาน ในคำขอ ให้ตั้งค่าพารามิเตอร์ jobId
เป็นรหัสงานของรายงานที่ต้องการดึงข้อมูล
คุณกรองรายการรายงานได้โดยใช้พารามิเตอร์ต่อไปนี้อย่างน้อย 1 รายการ
-
ใช้พารามิเตอร์ createdAfter
เพื่อระบุว่า API ควรแสดงเฉพาะรายงานที่สร้างขึ้นหลังจากเวลาที่ระบุ คุณใช้พารามิเตอร์นี้เพื่อให้แน่ใจว่า API จะแสดงเฉพาะรายงานที่คุณยังไม่ได้ประมวลผล
-
ใช้พารามิเตอร์ startTimeBefore
เพื่อระบุว่าการตอบกลับของ API ควรมีเฉพาะรายงานที่ข้อมูลที่เก่าที่สุดในรายงานอยู่ก่อนวันที่ที่ระบุ ในขณะที่พารามิเตอร์ createdAfter
เกี่ยวข้องกับเวลาที่สร้างรายงาน วันที่นี้เกี่ยวข้องกับข้อมูลในรายงาน
-
ใช้พารามิเตอร์ startTimeAtOrAfter
เพื่อระบุว่าการตอบกลับของ API ควรมีเฉพาะรายงานที่มีข้อมูลที่เก่าที่สุดในรายงานเป็นวันที่ที่ระบุหรือหลังจากนั้น ค่าพารามิเตอร์นี้จะสอดคล้องกับข้อมูลในรายงาน ไม่ใช่เวลาที่สร้างรายงาน เช่นเดียวกับพารามิเตอร์ startTimeBefore
การตอบกลับของ API มีรายการทรัพยากร Report
สำหรับงานนั้น แหล่งข้อมูลแต่ละรายการจะอ้างอิงถึงรายงานที่มีข้อมูลสำหรับระยะเวลาที่ไม่ซ้ำกัน
- พร็อพเพอร์ตี้
startTime
และ endTime
ของทรัพยากรจะระบุระยะเวลาที่ข้อมูลของรายงานครอบคลุม
- พร็อพเพอร์ตี้
downloadUrl
ของทรัพยากรจะระบุ URL ที่ดึงข้อมูลรายงานได้
- พร็อพเพอร์ตี้
createTime
ของทรัพยากรจะระบุวันที่และเวลาที่สร้างรายงาน แอปพลิเคชันควรจัดเก็บค่านี้และใช้เพื่อพิจารณาว่ารายงานที่ดาวน์โหลดก่อนหน้านี้มีการเปลี่ยนแปลงหรือไม่
ขั้นตอนที่ 4: ดาวน์โหลดรายงาน
ส่งคำขอ HTTP GET ไปยัง downloadUrl
ที่ได้รับในขั้นตอนที่ 4 เพื่อดึงข้อมูลรายงาน
รายงานการประมวลผล
แนวทางปฏิบัติแนะนำ
แอปพลิเคชันที่ใช้ YouTube Reporting API ควรปฏิบัติตามแนวทางต่อไปนี้เสมอ
-
ใช้แถวส่วนหัวของรายงานเพื่อกำหนดลำดับของคอลัมน์ในรายงาน เช่น อย่าคิดว่ายอดดูจะเป็นเมตริกแรกที่แสดงในรายงานเพียงเพราะเป็นเมตริกแรกที่ระบุไว้ในคำอธิบายรายงาน แต่ให้ใช้แถวส่วนหัวของรายงานเพื่อระบุว่าคอลัมน์ใดมีข้อมูลนั้น
-
บันทึกรายงานที่คุณดาวน์โหลดไว้เพื่อหลีกเลี่ยงการประมวลผลรายงานเดียวกันซ้ำๆ รายการต่อไปนี้แสดงวิธีดำเนินการ 2 วิธี
-
เมื่อเรียกใช้เมธอด reports.list
ให้ใช้พารามิเตอร์ createdAfter เพื่อดึงข้อมูลเฉพาะรายงานที่สร้างขึ้นหลังจากวันที่ที่ระบุ (ละเว้นพารามิเตอร์ createdAfter
ในครั้งแรกที่คุณดึงข้อมูลรายงาน)
ทุกครั้งที่คุณดึงและประมวลผลรายงานสำเร็จ ให้จัดเก็บการประทับเวลาที่สอดคล้องกับวันที่และเวลาที่สร้างรายงานล่าสุด จากนั้นอัปเดตค่าพารามิเตอร์ createdAfter
ในการเรียกใช้เมธอด reports.list
แต่ละครั้งเพื่อให้แน่ใจว่าคุณจะเรียกข้อมูลเฉพาะรายงานใหม่ รวมถึงรายงานใหม่ที่มีข้อมูลที่เติมแบ็กเท่านั้นทุกครั้งที่เรียกใช้ API
เพื่อเป็นการป้องกัน ก่อนที่จะดึงข้อมูลรายงาน ให้ตรวจสอบด้วยว่ารหัสของรายงานไม่ได้อยู่ในฐานข้อมูลของคุณแล้ว
-
จัดเก็บรหัสของรายงานแต่ละรายการที่คุณดาวน์โหลดและประมวลผล คุณยังจัดเก็บข้อมูลเพิ่มเติมได้ เช่น วันที่และเวลาที่สร้างรายงานแต่ละฉบับ หรือstartTime
และendTime
ของรายงาน ซึ่งระบุระยะเวลาที่รายงานมีข้อมูลร่วมกัน สำหรับรายงานที่ดึงข้อมูลจำนวนมากสำหรับข้อมูลวิเคราะห์ YouTube แต่ละงานมักจะมีรายงานหลายรายการ เนื่องจากแต่ละรายงานมีข้อมูลสำหรับระยะเวลา 24 ชั่วโมง งานที่ระบบจัดการซึ่งครอบคลุมระยะเวลาที่นานขึ้นจะมีรายงานน้อยลง
ใช้รหัสรายงานเพื่อระบุรายงานที่คุณยังต้องดาวน์โหลดและนำเข้า อย่างไรก็ตาม หากรายงานใหม่ 2 รายการมีค่าพร็อพเพอร์ตี้ startTime
และ endTime
เหมือนกัน ให้นําเข้ารายงานที่มีค่า createTime
ใหม่กว่าเท่านั้น
ลักษณะของรายงาน
รายงาน API เป็นไฟล์ .csv
(ค่าที่คั่นด้วยคอมมา) ที่มีการกำหนดเวอร์ชันซึ่งมีลักษณะดังนี้
-
รายงานแต่ละฉบับจะมีข้อมูลสำหรับระยะเวลาที่ไม่ซ้ำกันซึ่งเริ่มตั้งแต่เวลา 00:00 น. ตามเวลาแปซิฟิกในวันที่เริ่มต้นของรายงานจนถึงเวลา 23:59 น. ตามเวลาแปซิฟิกในวันที่สิ้นสุดของรายงาน
-
ไม่ได้จัดเรียงข้อมูลรายงาน
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-21 UTC
[null,null,["อัปเดตล่าสุด 2025-08-21 UTC"],[[["\u003cp\u003eYouTube's Reporting API provides access to system-managed ad revenue reports, which differ from Creator Studio reports in terms of fields and naming, yet contain similar data.\u003c/p\u003e\n"],["\u003cp\u003eRetrieving these reports involves four steps: getting authorization credentials, retrieving the job ID, getting the report's download URL, and downloading the report.\u003c/p\u003e\n"],["\u003cp\u003eThe API requires specific authorization scopes, either \u003ccode\u003ehttps://www.googleapis.com/auth/yt-analytics.readonly\u003c/code\u003e for general user activity metrics or \u003ccode\u003ehttps://www.googleapis.com/auth/yt-analytics-monetary.readonly\u003c/code\u003e for monetary and ad performance metrics.\u003c/p\u003e\n"],["\u003cp\u003eBest practices for using the API include using the header row to identify column ordering and keeping a record of downloaded reports to avoid reprocessing the same data, while also checking for updated reports.\u003c/p\u003e\n"],["\u003cp\u003eEach report is a \u003ccode\u003e.csv\u003c/code\u003e file containing data for a specific period, from 12:00 a.m. to 11:59 p.m. Pacific Time, and the data within the reports is not sorted.\u003c/p\u003e\n"]]],["YouTube provides system-managed ad revenue reports accessible via the Reporting API. To retrieve these reports, first, obtain authorization credentials using OAuth 2.0. Next, retrieve the job ID for the desired report type by calling `jobs.list` with `includeSystemManaged` set to `true`. Then, call `jobs.reports.list`, providing the job ID, to get the report's download URL, which may be filtered by creation or start times. Finally, use an HTTP GET request to download the report. Remember to store downloaded report details to avoid reprocessing.\n"],null,["# YouTube Reporting API - Get Bulk Data Reports\n\nYouTube automatically generates a set of system-managed ad revenue reports for content owners that have access to the corresponding reports in [Creator Studio](http://studio.youtube.com/). These reports are designed to provide programmatic access to data that is also available in manually downloadable reports accessible in the [Reports menu](https://support.google.com/youtube/answer/7648605) of the YouTube Creator Studio.\n\n**Note:** The API provides access to a different set of reports than Creator Studio, though the reports contain similar data. API reports might have different fields and also use different field names than Creator Studio reports.\n\nSince YouTube automatically generates system-managed reports, the process for retrieving these reports is different than for the YouTube Analytics bulk data reports available via the API.\n\nRetrieving reports\n------------------\n\nThe following steps explain how to retrieve system-managed reports via the API.\n\n### Step 1: Retrieve authorization credentials\n\nAll YouTube Reporting API requests must be authorized. The [Authorization guide](/youtube/reporting/guides/authorization) explains how to use the OAuth 2.0 protocol to retrieve authorization tokens.\n\nYouTube Reporting API requests use the following authorization scopes:\n\n| Scopes ||\n|----------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| https://www.googleapis.com/auth/yt-analytics.readonly | View YouTube Analytics reports for your YouTube content. This scope provides access to user activity metrics, like view counts and rating counts. |\n| https://www.googleapis.com/auth/yt-analytics-monetary.readonly | View YouTube Analytics monetary reports for your YouTube content. This scope provides access to user activity metrics and to estimated revenue and ad performance metrics. |\n\n### Step 2: Retrieve the job ID for the desired report\n\nCall the `jobs.list` method to retrieve a list of system-managed jobs. Set the [includeSystemManaged](/youtube/reporting/v1/reference/rest/v1/jobs/list#includeSystemManaged) parameter to `true`.\n\nThe [reportTypeId](/youtube/reporting/v1/reference/rest/v1/jobs#reportTypeId) property in each returned `Job` resource identifies the type of system-managed report associated with that job. Your application needs the [id](/youtube/reporting/v1/reference/rest/v1/jobs#id) property value from the same resource in the following step.\n\nThe [Reports](/youtube/reporting/v1/reports/system_managed/reports) document lists available reports, their report type IDs, and the fields they contain. You can also use the [reportTypes.list](/youtube/reporting/v1/reference/rest/v1/reportTypes/list) method to retrieve a list of supported report types.\n\n### Step 3: Retrieve the report's download URL\n\nCall the `jobs.reports.list` method to retrieve a list of reports created for the job. In the request, set the [jobId](/youtube/reporting/v1/reference/rest/v1/jobs.reports/list#jobId) parameter to the job ID of the report that you want to retrieve.\n\nYou can filter the list of reports using any or all of the following parameters:\n\n- Use the [createdAfter](/youtube/reporting/v1/reference/rest/v1/jobs.reports/list#createdAfter) parameter to indicate that the API should only return reports created after a specified time. This parameter can be used to ensure that the API only returns reports that you have not already processed.\n\n- Use the [startTimeBefore](/youtube/reporting/v1/reference/rest/v1/jobs.reports/list#startTimeBefore) parameter to indicate that the API response should only contain reports if the earliest data in the report is before the specified date. Whereas the `createdAfter` parameter pertains to the time the report was created, this date pertains to the data in the report.\n\n- Use the [startTimeAtOrAfter](/youtube/reporting/v1/reference/rest/v1/jobs.reports/list#startTimeAtOrAfter) parameter to indicate that the API response should only contain reports if the earliest data in the report is on or after the specified date. Like the `startTimeBefore` parameter, this parameter value corresponds to the data in the report and not the time the report was created.\n\nThe API response contains a list of `Report` resources for that job. Each resource refers to a report that contains data for a unique period.\n\n- The resource's [startTime](/youtube/reporting/v1/reference/rest/v1/jobs.reports#startTime) and [endTime](/youtube/reporting/v1/reference/rest/v1/jobs.reports#endTime) properties identify the time period that the report's data covers.\n- The resource's [downloadUrl](/youtube/reporting/v1/reference/rest/v1/jobs.reports#downloadUrl) property identifies the URL from which the report can be fetched.\n- The resource's [createTime](/youtube/reporting/v1/reference/rest/v1/jobs.reports#createTime) property specifies the date and time when the report was generated. Your application should store this value and use it to determine whether previously downloaded reports have changed.\n\n### Step 4: Download the report\n\nSend an HTTP GET request to the `downloadUrl` obtained in step 4 to retrieve the report.\n\nProcessing reports\n------------------\n\n### Best practices\n\nApplications that use the YouTube Reporting API should *always* follow these practices:\n\n- Use a report's header row to determine the ordering of the report's columns. For example, do not assume that [views](/youtube/reporting/v1/reports/metrics#views) will be the first metric returned in a report just because it is the first metric listed in a report description. Instead, use the report's header row to determine which column contains that data.\n\n- Keep a record of the reports you have downloaded to avoid repeatedly processing the same report. The following list suggests a couple of ways to do that.\n\n - When calling the [reports.list](/youtube/reporting/v1/reference/rest/v1/jobs.reports/list) method, use the [createdAfter](/youtube/reporting/v1/reference/rest/v1/jobs.reports/list#createdAfter) parameter to only retrieve reports created after a certain date. (Omit the `createdAfter` parameter the first time you retrieve reports.)\n\n Each time you retrieve and successfully process reports, store the timestamp corresponding to the [date and time](/youtube/reporting/v1/reference/rest/v1/jobs.reports#createTime) when the newest of those reports was created. Then, update the `createdAfter` parameter value on each successive call to the [reports.list](/youtube/reporting/v1/reference/rest/v1/jobs.reports/list) method to ensure that you are only retrieving new reports, including new reports with backfilled data, each time you call the API.\n\n As a safeguard, before retrieving a report, also check to ensure that the report's [ID](/youtube/reporting/v1/reference/rest/v1/jobs.reports#id) is not already listed in your database.\n - Store the [ID](/youtube/reporting/v1/reference/rest/v1/jobs.reports#id) for each report that you have downloaded and processed. You can also store additional information like the [date and time](/youtube/reporting/v1/reference/rest/v1/jobs.reports#createTime) when each report was generated or the report's [startTime](/youtube/reporting/v1/reference/rest/v1/jobs.reports#startTime) and [endTime](/youtube/reporting/v1/reference/rest/v1/jobs.reports#endTime), which together identify the period for which the report contains data. For reports that retrieve bulk data for YouTube Analytics, each job will likely have many reports since each report contains data for a 24-hour period. System-managed jobs that cover longer time periods will have fewer reports.\n\n Use the report ID to identify reports that you still need to download and import. However, if two new reports have the same [startTime](/youtube/reporting/v1/reference/rest/v1/jobs.reports#startTime) and [endTime](/youtube/reporting/v1/reference/rest/v1/jobs.reports#endTime) property values, only import the report with the newer [createTime](/youtube/reporting/v1/reference/rest/v1/jobs.reports#createTime) value.\n\n### Report characteristics\n\nAPI reports are versioned `.csv` (comma-separated values) files that have the following characteristics:\n\n- Each report contains data for a unique period lasting from 12:00 a.m. Pacific time on the report's start date through 11:59 p.m. Pacific time on the report's end date.\n\n- Report data is not sorted."]]