查看查詢記錄稽核

對於使用廣告資料中心帳戶時執行的所有工作,您可以透過「查詢記錄稽核」功能產生相關報表,進一步瞭解資料的存取者和存取時間。

查詢記錄稽核會寫成 BigQuery 資料表,內含的記錄項目會列出使用廣告資料中心帳戶執行的所有查詢。如要查看帳戶的查詢記錄稽核項目,您必須先透過 API 產生報表。每個稽核記錄都包含 1 天的資料。您可以針對過去 30 天內的任何日期產生稽核記錄。

請注意,只有超級使用者才能使用查詢記錄稽核功能。進一步瞭解角色型存取權

查詢記錄稽核格式

每個查詢記錄稽核都會採用以下結構定義:

欄位名稱 說明
customer_id 廣告資料中心客戶 ID
ads_customer_id 子帳戶 ID (如已使用,否則這欄的值會與 customer_id 相同)
Match_table_customer_id 內含對照表的帳戶 ID (如有使用,否則這欄的值會與 customer_id 相同)
user_email 執行查詢的使用者電子郵件地址
query_start_time 查詢開始執行的時間
query_end_time 查詢執行完成的時間
query_type 區別分析查詢和目標對象查詢
query_resource_id 與查詢相關聯的 ID
query_text 查詢的 SQL
query_parameters
query_parameters.name 查詢參數的名稱
query_parameters.value 透過查詢的參數「row_merge_summary」傳送的值
row_merge_summary.column_name 資料欄名稱
row_merge_summary.merge_type 資料列合併摘要類型
row_merge_summary.constant_value 常數集的值 (如未使用常數,則為空值)
destination_table 查詢寫入的位置 (在 BigQuery 中)

存取查詢記錄稽核功能

如要存取查詢記錄稽核功能,您必須呼叫 API。請參閱下方的 API 呼叫程式碼範例,或查看參考說明文件並自行撰寫查詢。

API 要求的結果會寫入您在 API 要求內文中指定的 BigQuery 資料集。


"""This sample shows how to create a query history audit.

For the program to execute successfully, ensure that you run it using Python 3.
"""

from __future__ import print_function
from json import dumps
from google_auth_oauthlib import flow
from googleapiclient.discovery import build

appflow = flow.InstalledAppFlow.from_client_secrets_file(
  # Replace client_secrets.json with your own client secret file.
  'client_secrets.json',
  scopes=['https://www.googleapis.com/auth/adsdatahub'])
appflow.run_local_server()
credentials = appflow.credentials
developer_key = input('Developer key: ').strip()
service = build('adsdatahub', 'v1', credentials=credentials,
                developerKey=developer_key)

def pprint(x):
  print(dumps(x, sort_keys=True, indent=4))

customer_id = input('Customer ID (e.g. "customers/123"): ').strip()
bq_project = input('Destination BigQuery project ID (e.g. "your-project"): ').strip()
dataset_id = input('Destination BigQuery dataset (e.g. "your-dataset"): ').strip()
start = input('The start date for your query history audit. Formatted as "mm/dd/yyyy": ').strip().split('/')
end = input('The end date for your query history audit. Should be 1 day later than start_date. Formatted as "mm/dd/yyyy": ').strip().split('/')

choice = input("Do you want to enter a timezone? Defaults to UTC otherwise. (y/n) ")

if choice.lower() == 'y':
  timezone = input("Timezone (e.g. 'UTC'): ")
else:
  timezone = 'UTC'

body = {
  'project_id': bq_project,
  'dataset': dataset_id,
  'start_date': {
      'year': start[2],
      'day': start[1],
      'month': start[0]
  },
  'end_date': {
      'year': end[2],
      'day': end[1],
      'month': end[0]
  },
  'time_zone': timezone
}

pprint(service.customers().exportJobHistory(customer=customer_id, body=body).execute())