Integrating with Google Analytics

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

Analytics collection can take place on both the client and server side. Google Analytics provides easy to use APIs and SDKs to send data to Google Analytics. In addition to those, we have developed code that you can use in your App Engine applications to easily send server-side analytics to Google Analytics.

Client-side analytics collection

With the collection APIs and SDKs, you can measure how users interact with your content and marketing initiatives. Once implemented, you will be able to view user-interaction data within Google Analytics or through the Reporting APIs. 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.

App Engine server-side analytics collection

Although App Engine already provides a mechanism for logging events in your application, it may be advantageous to track specific server-side events in Google Analytics. Some of the benefits are as follows:

  • 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.
  • 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.
  • Powerful user interface - Take advantage of the rich user interface that Google Analytics provides to visualize, report and export these server side events.

This can be accomplished easily by integrating the sample source code below into your App Engine application. For additional information on this approach consult the Google Analytics developers guide for Event Tracking.

Sample source code

import com.google.appengine.api.urlfetch.URLFetchService;
import com.google.appengine.api.urlfetch.URLFetchServiceFactory;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.http.client.utils.URIBuilder;

@SuppressWarnings("serial")
// With @WebServlet annotation the webapp/WEB-INF/web.xml is no longer required.
@WebServlet(
    name = "analytics",
    description = "Analytics: Send Analytics Event to Google Analytics",
    urlPatterns = "/analytics"
)
public class AnalyticsServlet extends HttpServlet {

  @Override
  public void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws IOException, ServletException {
    String trackingId = System.getenv("GA_TRACKING_ID");
    URIBuilder builder = new URIBuilder();
    builder
        .setScheme("http")
        .setHost("www.google-analytics.com")
        .setPath("/collect")
        .addParameter("v", "1") // API Version.
        .addParameter("tid", trackingId) // Tracking ID / Property ID.
        // Anonymous Client Identifier. Ideally, this should be a UUID that
        // is associated with particular user, device, or browser instance.
        .addParameter("cid", "555")
        .addParameter("t", "event") // Event hit type.
        .addParameter("ec", "example") // Event category.
        .addParameter("ea", "test action"); // Event action.
    URI uri = null;
    try {
      uri = builder.build();
    } catch (URISyntaxException e) {
      throw new ServletException("Problem building URI", e);
    }
    URLFetchService fetcher = URLFetchServiceFactory.getURLFetchService();
    URL url = uri.toURL();
    fetcher.fetch(url);
    resp.getWriter().println("Event tracked.");
  }
}