Adobe Flex Setup

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

Get the Tracking Library

Download the code (ZIP format) from http://code.google.com/p/gaforflash/downloads/list. Follow the instructions in the readme.txt file to install the Flex component. Unlike the Analytics Flash components, there is only one library file located in the download: /lib/analytics_flex.swc.

Add the Code to Your Project

Before you can start using the tracking code in your project, you will need to link the SWC file you downloaded as a project resource.

A Flex MXML Component Example

The following example shows how to set up tracking for a button inside an MXML file. In order to initialize the MXML component, do the following:

Set the XML namespace parameter. Set the XML namespace parameter of your tracking object as follows: xmlns:analytics="com.google.analytics.components.*"

Set the object name. In the example below, the id parameter is set to tracker.

Use the account parameter for 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.

Set the tracking mode with the mode parameter. Use Bridge for the parameter if you are creating an MXML file and want a simple way to implement tracking. Use AS3 if you are comfortable coding using ActionScript3. AS3 mode allows you to import all the GA tracking classes, from which you can create and configure your own tracking objects.

Set the debug mode with the visualDebug parameter. Use true to turn on debugging and validation for your program; otherwise, set this to false for production use.

In the example, a button mybutton is added to the stage. The Flex tracking component is instantiated with the name tracker and configured with its parameters. Finally, a click event onButtonClick is added to mybutton. When the button is clicked, the virtual pageview /hello world is incremented with a single count.

A Flex ActionScript 3 Example

In more complex situations, you might want to call the native ActionScript 3 tracking classes directly from your ActionScript resources files. While this example is also an MXML file, all the tracking instantiation is done in the <MX:script> tags. This same process can be used to add tracking to other ActionScript 3 projects.

In this example, a button mybutton is added to the stage. In the script tags, two libraries are imported:

com.google.analytics.GATracker; //this is the actual tracking class
com.google.analytics.AnalyticsTracker; //this is an interface that the GATracker class implements

After the libraries are imported, the tracking variable tracker is created. Once the application has been added to the stage, onComplete is called, which instantiates the tracking object. The four parameters that you need to instantiate the GATracker object are:

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.