This document covers everything you need to know to set up Google Analytics Tracking for Adobe Flash for the Adobe Flex development environment.
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.
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.
lib/analytics.swc file and click OK.analytics.swc file into your Flex project /libs directory.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:
xmlns:analytics="com.google.analytics.components.*"id parameter is set to tracker.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 profile in your Analytics account.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. 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.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute" width="800" height="600"
>
<mx:Script>
public function onButtonClick():void
{
tracker.trackPageview( "/hello world" );
}
</mx:Script>
<analytics:FlexTracker
xmlns:analytics="com.google.analytics.components.*"
id="tracker"
account="UA-111-222"
mode="AS3"
visualDebug="false"
/>
<mx:Button id="mybutton" label="hello world" click="onButtonClick()" />
</mx:Application>
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:
this references the display object. Bridge or AS3. debug to false for production use and to true for validation and troubleshooting.Finally, on the myButton click method, the onButtonClick function tracks a virtual pageview on our tracking object.
When this application runs, every time a user clicks the button, a virtual pageview of "hello world" is sent to the Google Analytics tracking servers.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
width="800" height="600"
addedToStage="onComplete()"
>
<mx:Script>
<![CDATA[
import com.google.analytics.GATracker;
import com.google.analytics.AnalyticsTracker;
public var tracker:AnalyticsTracker;
private function onComplete():void
{
tracker = new GATracker( this, "UA-111-222", "AS3", false );
}
public function onButtonClick():void
{
tracker.trackPageview( "/hello/world" );
}
]]>
</mx:Script>
<mx:Button id="mybutton" label="hello world" click="onButtonClick()" />
</mx:Application>