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 Google App Engine:

You can integrate the following code into your App Engine application to post event tracking data to Google Analytics:

// Copyright 2015 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.

// Sample analytics demonstrates Google Analytics calls from App Engine flexible environment.
package main

import (



var gaPropertyID = mustGetenv("GA_TRACKING_ID")

func mustGetenv(k string) string {
	v := os.Getenv(k)
	if v == "" {
		log.Fatalf("%s environment variable not set.", k)
	return v

func main() {
	http.HandleFunc("/", handle)


func handle(w http.ResponseWriter, r *http.Request) {
	if r.URL.Path != "/" {
		http.NotFound(w, r)

	if err := trackEvent(r, "Example", "Test action", "label", nil); err != nil {
		fmt.Fprintf(w, "Event did not track: %v", err)
	fmt.Fprint(w, "Event tracked.")

func trackEvent(r *http.Request, category, action, label string, value *uint) error {
	if gaPropertyID == "" {
		return errors.New("analytics: GA_TRACKING_ID environment variable is missing")
	if category == "" || action == "" {
		return errors.New("analytics: category and action are required")

	v := url.Values{
		"v":   {"1"},
		"tid": {gaPropertyID},
		// Anonymously identifies a particular user. See the parameter guide for
		// details:
		// Depending on your application, this might want to be associated with the
		// user in a cookie.
		"cid": {uuid.NewV4().String()},
		"t":   {"event"},
		"ec":  {category},
		"ea":  {action},
		"ua":  {r.UserAgent()},

	if label != "" {
		v.Set("el", label)

	if value != nil {
		v.Set("ev", fmt.Sprintf("%d", *value))

	if remoteIP, _, err := net.SplitHostPort(r.RemoteAddr); err != nil {
		v.Set("uip", remoteIP)

	// NOTE: Google Analytics returns a 200, even if the request is malformed.
	_, err := http.PostForm("", v)
	return err

Send feedback about...

App Engine flexible environment for Go