Python 2.7 已达到支持终止期限,并将于 2026 年 1 月 31 日
弃用。弃用后,您将无法部署 Python 2.7 应用,即使您的组织之前曾使用组织政策重新启用旧版运行时的部署也是如此。现有的 Python 2.7 应用在
弃用日期之后将继续运行并接收流量。我们建议您
迁移到最新支持的 Python 版本。
集成 Google Analytics
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
借助 Google Analytics 平台,您可以跨多种设备和环境衡量用户与您的企业互动的情况。该平台会提供收集、存储、处理和报告上述用户互动所需的所有计算资源。
Analytics 数据收集在客户端和服务器端都可以进行。Google Analytics 提供易用的 API 和 SDK,以便将数据发送到 Google Analytics。除此之外,我们还开发了可在 App Engine 应用中使用的代码,可以轻松地将服务器端分析发送到 Google Analytics。
客户端分析数据收集
借助数据收集 API 和 SDK,您可以衡量用户与您的内容和营销计划的互动方式。实现数据收集功能后,您将能够在 Google Analytics 中或通过 Reporting API 查看用户互动数据。如需详细了解客户端分析数据收集,请根据您的客户端类型选择以下链接:
App Engine 服务器端分析数据收集
虽然 App Engine 已经提供在应用中记录事件的机制,但在 Google Analytics 中跟踪特定的服务器端事件可能更有利。部分优势如下:
- 历史数据分析 - 借助 App Engine,您可以配置日志文件最长保留天数或最大大小。超过该天数后,您将无权再访问这些日志文件。借助 Google Analytics 中的事件跟踪功能,您可以在更长的有效期内查看过往事件。
- 跟踪关键事件 - 应用的各种组件都会向日志文件写入数据,因而可能导致日志文件过于冗长。借助事件跟踪,您可以仅精确定位想要监控的关键事件,并跟踪这些事件以及一些其他元数据。
- 强大的用户界面 - 利用 Google Analytics 提供的功能丰富的用户界面,可以直观呈现、报告和导出这些服务器端事件。
将以下示例源代码集成到 App Engine 应用中,可以轻松实现此功能。如需详细了解此方法,请参阅 Google Analytics 开发者指南中的事件跟踪。
示例源代码
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-09-04。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-04。"],[[["\u003cp\u003eThe Google Analytics Platform offers comprehensive resources for measuring user interactions across various devices and environments, covering data collection, storage, processing, and reporting.\u003c/p\u003e\n"],["\u003cp\u003eAnalytics collection can be performed on both the client and server sides, with Google Analytics providing APIs and SDKs for easy data transmission, along with pre-developed code for App Engine server-side analytics.\u003c/p\u003e\n"],["\u003cp\u003eClient-side analytics allows the tracking of user interaction with content and marketing efforts through Web Tracking, Android, iOS, and Measurement Protocol.\u003c/p\u003e\n"],["\u003cp\u003eServer-side event tracking in Google Analytics provides benefits over standard App Engine logs, including historical data analysis, focused tracking of key events, and a user-friendly interface for data visualization and reporting.\u003c/p\u003e\n"],["\u003cp\u003eThe sample code provided demonstrates how to integrate event tracking into App Engine applications, sending data directly to Google Analytics for analysis.\u003c/p\u003e\n"]]],[],null,["# Integrating with Google Analytics\n\nThe [Google Analytics Platform](/analytics/devguides/platform)\nlets you measure user interactions with your business across various devices and\nenvironments. The platform provides all the computing resources to collect,\nstore, process, and report on these user-interactions.\n\n[Analytics collection](/analytics/devguides/collection)\ncan take place on both the client and server side. Google Analytics provides\neasy to use APIs and SDKs to send data to Google Analytics. In addition to\nthose, we have developed code that you can use in your App Engine applications\nto easily send server-side analytics to Google Analytics.\n\nClient-side analytics collection\n--------------------------------\n\nWith the collection APIs and SDKs, you can measure how users interact with your\ncontent and marketing initiatives. Once implemented, you will be able to view\nuser-interaction data within Google Analytics or through the Reporting APIs.\nFor more details on client-side analytics collection select the link\nbelow based on the type of your client:\n\n- [Web Tracking (analytics.js)](/analytics/devguides/collection/analyticsjs) - Measure user interaction with websites or web applications.\n- [Android](/analytics/devguides/collection/android) - Measure user interaction with Android applications.\n- [iOS](/analytics/devguides/collection/ios) - Measure user interaction with iOS applications.\n- [Measurement Protocol](/analytics/devguides/collection/protocol/v1) - Measure user interaction in any environment with this low-level protocol.\n\nApp Engine server-side analytics collection\n-------------------------------------------\n\nAlthough App Engine already provides a mechanism for\n\n[logging events](/appengine/docs/legacy/standard/python/logs)\n\nin your application, it may be advantageous to track specific server-side events\nin Google Analytics. Some of the benefits are as follows:\n\n- **Historical data analysis** - App Engine allows you to configure the maximum number of days, or size of your log file. After that time has passed you no longer have access to those log files. Tracking events in Google Analytics provides you a much longer lifespan into the visibility of past events.\n- **Track key events** - Log files can be verbose with various components of your application writing data to them. By using event tracking you can pinpoint just the key events that you are interested in monitoring and track those along with some additional metadata.\n- **Powerful user interface** - Take advantage of the rich user interface that Google Analytics provides to visualize, report and export these server side events.\n\n\nThis can be accomplished easily by integrating the sample source code below\ninto your App Engine application. For additional information\non this approach consult the Google Analytics developers guide for\n[Event Tracking](/analytics/devguides/collection/protocol/v1/devguide#event).\n\n\u003cbr /\u003e\n\n### Sample source code\n\n def track_event(category, action, label=None, value=0):\n data = {\n 'v': '1', # API Version.\n 'tid': GA_TRACKING_ID, # Tracking ID / Property ID.\n # Anonymous Client Identifier. Ideally, this should be a UUID that\n # is associated with particular user, device, or browser instance.\n 'cid': '555',\n 't': 'event', # Event hit type.\n 'ec': category, # Event category.\n 'ea': action, # Event action.\n 'el': label, # Event label.\n 'ev': value, # Event value, must be an integer\n }\n\n response = requests.post(\n 'http://www.google-analytics.com/collect', data=data)\n\n # If the request fails, this will raise a RequestException. Depending\n # on your application's needs, this may be a non-error and can be caught\n # by the caller.\n response.raise_for_status()\n\n\n @app.route('/')\n def track_example():\n track_event(\n category='Example',\n action='test action')\n return 'Event tracked.'\n\n\u003cbr /\u003e"]]