Adobe Flash Setup

This document covers everything you need to know to set up Google Analytics Tracking for Adobe Flash for the Adobe Flash development environment.

Get the Component Files

The Flash tracking component files are compressed into a single ZIP file that you can download from http://code.google.com/p/gaforflash/downloads/list.

Follow the instructions in the readme.txt file to install the Flash components.

  1. If you have Adobe Flash CS3 currently open, quit the application.
  2. Create a Google directory in one of the following locations:
    • For Windows: C:\Program Files\Adobe\ Adobe Flash CS3\language\Configuration\Components
    • For Mac OS X: Macintosh HD/Applications/Adobe Flash CS3/Configuration/Components
  3. Navigate to the location where you unzipped the component ZIP file and copy the following files to the directory you created in the previous step:
    • lib/analytics_flash.swc — The Analytics Component
    • lib/analytics.swc — The Analytics Library Component

Both components have the same functionality, but are provided as a convenience for different development styles.

Analytics component. If you develop Flash content, but are unfamiliar with ActionScript 3, use this component. The Analytics component is a complete tracking package. Just drag and drop the components directly onto your stage, make a few simple configurations in the component inspector, and you're ready to tag your controls with Analytics tracking.

AnalyticsLibrary component. If you are familiar with ActionScript 3, you can use the AnalyticLibrary component. Drag the component to your Flash Library and import the tracking classes directly into your ActionScript code.

Using The Analytics Component

Use the analytics_flash.swc to implement tracking via the Analytics component. Place the component in the correct Component directory, and it appears in the Components panel (Window - Components).

  1. Drag the component to the stage and give it an instance name in the Properties dialog.
  2. Configure the component in the Component Inspector panel (Windows - Component Inspector):
    • Add your Analytics web property ID.
    • Select either Bridge mode or AS3 mode.
    • Indicate whether visual debug is turned on or off.
  3. In your ActionScript code, add the standard GA tracking calls to the instance of the component you just created.

Because of the way Flash works with visual components, it's generally best to start tracking on the second frame of your Flash application. If you need to initialize tracking on the first frame, place the tracking calls inside event handlers to ensure the component is fully initialized within the Flash content.

A Simple Analytics Flash Component Example

The following code snippet shows how a button in Flash can be tracked when someone clicks it. In this example, the button instance is named playGame in your ActionScript. When the button is clicked, the onButtonClick method is called, which in turn invokes the trackPageview() method. This increments the pageview count for the virtual page /myGame1.

playGame.addEventListener( MouseEvent.CLICK, onButtonClick );
function onButtonClick( event:Event ):void
{
  tracker.trackPageview( "/myGame1");
} 

Using The AnalyticsLibrary Component

Use the analytics.swc file to implement tracking via the AnalyticsLibrary component. This component lets you instantiate a tracking object directly in your AS3 code. Once the AnalyticsLibrary component is in the proper location, it appears in the Components panel. You can then drag the component from the Components panel into your library, and from there import the libraries into your ActionScript code.

Initialize the Tracking Object

Using the Adobe Flash environment, initialize the tracking object with the following parameters:

  • Reference the current display object. In the example below, this references the display object.
  • The web property ID. The web property ID is the unique string used to track activity on your Flash content and deliver it to the correct view (profile) in your Analytics account.
  • The tracking mode. Valid parameters are Bridge or AS3.
  • The debug mode. Set debug to false for production use and to true for validation and troubleshooting.

A Simple AnalyticsLibrary Example

In the following example, a movie clip called playGame exists on the stage. A new tracking object called tracker is created along with an event handler that listens for a mouse click. When the button is clicked, the onButtonClick function is called. For each click, the trackPageview() method increments the pageview count for the virtual page /myGame1.

import com.google.analytics.AnalyticsTracker;
import com.google.analytics.GATracker;
var tracker:AnalyticsTracker = new GATracker( this, "UA-111-222", "AS3", true );
playGame.addEventListener( MouseEvent.CLICK, onButtonClick );
function onButtonClick ( event:Event ):void
{
   tracker.trackPageview("/myGame1");
}