Để truy xuất các chỉ số cho một ngày cụ thể, hãy gọi domains.trafficStats.get() bằng miền và ngày. Sau đây là một đoạn mã mẫu cho biết cách truy xuất các chỉ số về email cho một ngày cụ thể:
Java
/** * Gets the traffic stats for a domain for a specific date. * * @param service Authorized Gmail PostmasterTools API instance. * @param domainName The fully qualified domain name. * @param date The date to get the domain traffic stats. Must be in "YYYYMMDD" format. * @return The traffic stats of the domain for this date. * @throws IOException */publicstaticTrafficStatsgetTrafficStats(PostmasterToolsservice,StringdomainName,Stringdate)throwsIOException{Stringquery=String.format("domains/%s/trafficStats/%s",domainName,date);TrafficStatstrafficStats=service.domains().trafficStats().get(query).execute();System.out.println(trafficStats.toPrettyString());returntrafficStats;}
Python
"""Gets the traffic stats for a domain for a specific date. Args: service: Authorized Gmail PostmasterTools API instance. domain_name: The fully qualified domain name. date The date to get the domain traffic stats. Must be in "YYYYMMDD" format. Returns: The traffic stats of the domain for this date. """defget_traffic_stats(service,domain_name,date):"""Gets the traffic stats for a domain for a specific date. Args: service: Authorized Gmail PostmasterTools API instance. domain_name: The fully qualified domain name. date The date to get the domain traffic stats. Must be in "YYYYMMDD" format. Returns: The traffic stats of the domain for this date. """try:query='domains/%s/trafficStats/%s'%(domain_name,date)traffic_stats=service.domains().trafficStats().get(name=query).execute();print(traffic_stats);returntraffic_stats;excepterrors.HttpErroraserr:print('An error occurred: %s'%err)
Nếu thành công, nội dung phản hồi sẽ chứa một phiên bản của TrafficStats.
Truy xuất chỉ số cho tất cả các ngày
Để truy xuất các chỉ số cho tất cả các ngày, hãy gọi domains.trafficStats.list() bằng miền. Sau đây là một mã mẫu cho biết cách truy xuất các chỉ số về email cho tất cả các ngày:
Java
/** * Lists traffic statistics for all available days. * * @param service Authorized Gmail PostmasterTools API instance. * @param domainName The fully qualified domain name. * @param pageSize The number of TrafficStats to get per request. * @param pageToken The nextPageToken value returned from a previous List request, if any. * @return Response message for list traffic stats request. * @throws IOException */publicstaticListTrafficStatsResponselistTrafficStats(PostmasterToolsservice,StringdomainName,intpageSize,StringpageToken)throwsIOException{ListTrafficStatsResponselistTrafficStatsResponse=service.domains().trafficStats().list("domains/"+domainName).setPageSize(pageSize).setPageToken(pageToken).execute();System.out.println(listTrafficStatsResponse.toPrettyString());returnnull;}
Python
"""Gets the traffic stats for a domain for a specific date. Args: service: Authorized Gmail PostmasterTools API instance. domain_name: The fully qualified domain name. date The date to get the domain traffic stats. Must be in "YYYYMMDD" format. page_size The number of TrafficStats to get per request. page_token The nextPageToken value returned from a previous List request, if any. Returns: The traffic stats of the domain for this date. """deflist_traffic_stats(service,domain_name,date,page_size,page_token):"""Gets the traffic stats for a domain for a specific date. Args: service: Authorized Gmail PostmasterTools API instance. domain_name: The fully qualified domain name. date The date to get the domain traffic stats. Must be in "YYYYMMDD" format. page_size The number of TrafficStats to get per request. page_token The nextPageToken value returned from a previous List request, if any. Returns: The traffic stats of the domain for this date. """try:query='domains/'+domain_namelist_traffic_stats_response=service.domains().trafficStats().list(parent=query,pageSize=page_size,pageToken=page_token).execute();print(list_traffic_stats_response);returnlist_traffic_stats_response;excepterrors.HttpErroraserr:print('An error occurred: %s'%err)if__name__=='__main__':main()
Nếu thành công, nội dung phản hồi sẽ chứa một mảng được phân trang gồm TrafficStats có cấu trúc sau:
[null,null,["Cập nhật lần gần đây nhất: 2025-08-29 UTC."],[],[],null,["# Retrieve email metrics\n\nYou can retrieve email metrics for a specific day on a specific domain or for\nall days on a specific domain.\n\nFor information on how to improve certain metrics, refer to\n[Prevent mail to Gmail users from being blocked or sent to spam](https://support.google.com/mail/answer/81126)\n| **Note:** If you do not see values for some metrics, it's possible you do not send enough daily email. Only domains that send mail to at least 50 users per day receive metrics. For further information, refer to the [Postmaster Tools FAQ](https://support.google.com/mail/answer/6258950).\n\nRetrieve metrics for a specific day\n-----------------------------------\n\nTo retrieve metrics for a specific day, call\n[`domains.trafficStats.get()`](/workspace/gmail/postmaster/reference/rest/v1/domains.trafficStats/get)\nwith the domain and day. Following is a code sample showing how to retrieve\nemail metrics for a specific day: \n\n### Java\n\n /**\n * Gets the traffic stats for a domain for a specific date.\n *\n * @param service Authorized Gmail PostmasterTools API instance.\n * @param domainName The fully qualified domain name.\n * @param date The date to get the domain traffic stats. Must be in \"YYYYMMDD\" format.\n * @return The traffic stats of the domain for this date.\n * @throws IOException\n */\n public static TrafficStats getTrafficStats(PostmasterTools service, String domainName, String date) throws IOException {\n String query = String.format(\"domains/%s/trafficStats/%s\", domainName, date);\n TrafficStats trafficStats = service.domains().trafficStats().get(query).execute();\n System.out.println(trafficStats.toPrettyString());\n return trafficStats;\n }\n\n### Python\n\n \"\"\"Gets the traffic stats for a domain for a specific date.\n\n Args:\n service: Authorized Gmail PostmasterTools API instance.\n domain_name: The fully qualified domain name.\n date The date to get the domain traffic stats. Must be in \"YYYYMMDD\" format.\n\n Returns:\n The traffic stats of the domain for this date.\n \"\"\"\n def get_traffic_stats(service, domain_name, date):\n \"\"\"Gets the traffic stats for a domain for a specific date.\n\n Args:\n service: Authorized Gmail PostmasterTools API instance.\n domain_name: The fully qualified domain name.\n date The date to get the domain traffic stats. Must be in \"YYYYMMDD\" format.\n\n Returns:\n The traffic stats of the domain for this date.\n \"\"\"\n try:\n query = 'domains/%s/trafficStats/%s' %(domain_name,date)\n traffic_stats = service.domains().trafficStats().get(name=query).execute();\n print(traffic_stats);\n return traffic_stats;\n except errors.HttpError as err:\n print('An error occurred: %s' % err)\n\nIf successful, the response body contains an instance of [`TrafficStats`](/workspace/gmail/postmaster/reference/rest/v1/domains.trafficStats#TrafficStats).\n\nRetrieve metrics for all days\n-----------------------------\n\nTo retrieve metrics for all days, call\n[`domains.trafficStats.list()`](/workspace/gmail/postmaster/reference/rest/v1/domains.trafficStats/list)\nwith the domain. Following is a code sample showing how to retrieve email\nmetrics for all days: \n\n### Java\n\n /**\n * Lists traffic statistics for all available days.\n *\n * @param service Authorized Gmail PostmasterTools API instance.\n * @param domainName The fully qualified domain name.\n * @param pageSize The number of TrafficStats to get per request.\n * @param pageToken The nextPageToken value returned from a previous List request, if any.\n * @return Response message for list traffic stats request.\n * @throws IOException\n */\n public static ListTrafficStatsResponse listTrafficStats(PostmasterTools service, String domainName,\n int pageSize,\n String pageToken) throws IOException {\n ListTrafficStatsResponse listTrafficStatsResponse = service.domains().trafficStats().list(\"domains/\" + domainName)\n .setPageSize(pageSize)\n .setPageToken(pageToken)\n .execute();\n System.out.println(listTrafficStatsResponse.toPrettyString());\n return null;\n }\n\n### Python\n\n \"\"\"Gets the traffic stats for a domain for a specific date.\n\n Args:\n service: Authorized Gmail PostmasterTools API instance.\n domain_name: The fully qualified domain name.\n date The date to get the domain traffic stats. Must be in \"YYYYMMDD\" format.\n page_size The number of TrafficStats to get per request.\n page_token The nextPageToken value returned from a previous List request, if any.\n\n Returns:\n The traffic stats of the domain for this date.\n \"\"\"\n def list_traffic_stats(service, domain_name, date, page_size, page_token):\n \"\"\"Gets the traffic stats for a domain for a specific date.\n\n Args:\n service: Authorized Gmail PostmasterTools API instance.\n domain_name: The fully qualified domain name.\n date The date to get the domain traffic stats. Must be in \"YYYYMMDD\" format.\n page_size The number of TrafficStats to get per request.\n page_token The nextPageToken value returned from a previous List request, if any.\n\n Returns:\n The traffic stats of the domain for this date.\n \"\"\"\n try:\n query = 'domains/' + domain_name\n list_traffic_stats_response = service.domains().trafficStats().list(parent=query, pageSize=page_size, pageToken=page_token).execute();\n print(list_traffic_stats_response);\n return list_traffic_stats_response;\n except errors.HttpError as err:\n print('An error occurred: %s' % err)\n\n if __name__ == '__main__':\n main()\n\nIf successful, the response body contains a paginated array of [`TrafficStats`](/workspace/gmail/postmaster/reference/rest/v1/domains.trafficStats#TrafficStats) with the following structure: \n\n {\n \"trafficStats\": [\n {\n object (TrafficStats)\n }\n ],\n \"nextPageToken\": string\n }"]]