Notice: Over the next few months, we're reorganizing the App Engine documentation site to make it easier to find content and better align with the rest of Google Cloud products. The same content will be available, but the navigation will now match the rest of the Cloud products.

Integrating with Google Analytics

The Google Analytics Platform lets you measure user interactions with your business across various devices and environments. Google Analytics provides the resources to collect, store, process, and report on these user-interactions.

You can collect analytics on the client side and on the server side.

Client-side analytics collection

Use the Google Analytics APIs and SDKs to measure how users interact with your content and marketing initiatives. You can view user-interaction data in the Google Analytics user interface or use the Reporting APIs to fetch the data. For more details on client-side analytics collection, select the link below based on the type of your client:

  • Web Tracking (analytics.js) - Measure user interaction with websites or web applications.
  • Android - Measure user interaction with Android applications.
  • iOS - Measure user interaction with iOS applications.
  • Measurement Protocol - Measure user interaction in any environment with this low-level protocol.

Server-side analytics collection

Although App Engine provides a mechanism for logging events in your application, you can use Google Analytics to track specific server-side events in Google Analytics so that you can:

  • Analyze historical data - App Engine allows you to configure the maximum number of days, or size, of your log file. After that limit has been 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.
  • 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 key events and track them, along with some metadata.
  • Work in the user interface - Take advantage of the rich user interface that Google Analytics provides to visualize, report, and export these server-side events.

To enable server-side analytics collection, you can use an HTTP client and make HTTP requests using the Google Analytics Measurement Protocol. For additional information, consult the Google Analytics developers guide for Event Tracking.

Sample Application

The sample application below shows how to track events in your App Engine application. The application makes an HTTP request and posts data about the event to Google Analytics.

Before you use the Google Analytics Measurement Protocol on App Engine, follow these steps:

  1. Create a Universal Analytics property and obtain the Tracking ID.

  2. Include the environment variables in appsettings.json with your Tracking ID. For example, for the sample application shown below:

      "GaTrackingId": "your-google-analytics-tracking-id"

    Replace `your-google-analytics-tracking-id` with the tracking ID you
    obtained from Google Analytics.
    • To run the sample app locally:

    Visual Studio

    1. Open dotnet-docs-samples\appengine\flexible\AppEngineFlex.sln with Visual Studio.

    2. Press F5.

    Command Line

    1. Run the following commands from the dotnet-docs-samples\appengine\flexible\Analytics directory:

      dotnet restore
      dotnet run
    2. In your web browser, enter the following address:

    3. In your terminal window, press Ctrl+C to exit the web server.

  3. Integrate the following code into your App Engine application to post event tracking data to Google Analytics:

    HttpClient http = new HttpClient()
        BaseAddress = new Uri("")
    var content = new FormUrlEncodedContent(
        new Dictionary<string, string>() {
            { "v" , "1" },  // API Version.
            { "tid" , Configuration["GaTrackingId"] },  // Tracking ID / Property ID.
            // Anonymous Client Identifier. Ideally, this should be a UUID that
            // is associated with particular user, device, or browser instance.
            { "cid" , "555" },
            { "t" , "event" },  // Event hit type.
            { "ec" , "Poker" },  // Event category.
            { "ea" , "Royal Flush" },  // Event action.
            { "el" , "Hearts" },  // Event label.
            { "ev" , "0" },  // Event value, must be an integer
    var post = http.PostAsync("collect", content);

If you have enabled the setting in your Google Analytics Admin console to exclude traffic from spiders and bots, or if you are using Google Analytics 4 (which filters this traffic automatically), you might not see Analytics results from App Engine clients. These scenarios filter out the default App Engine user agent. To see App Engine results, include the ua parameter in your tracking data and set it to a custom value.