Skip to content

Commit

Permalink
use flex 4 as default and better error handling if account is set
Browse files Browse the repository at this point in the history
  • Loading branch information
srohde committed Jan 4, 2010
1 parent d8876bb commit e77efd7
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .actionScriptProperties
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<actionScriptProperties mainApplicationPath="FGATracker.as" projectUUID="6c0e244e-e174-4352-a7aa-8c16a236bbd7" version="6">
<compiler additionalCompilerArguments="-locale en_US" autoRSLOrdering="true" copyDependentFiles="false" generateAccessible="false" htmlExpressInstall="true" htmlGenerate="false" htmlHistoryManagement="false" htmlPlayerVersionCheck="true" includeNetmonSwc="false" outputFolderPath="bin" sourceFolderPath="src" strict="true" targetPlayerVersion="0.0.0" useApolloConfig="false" useDebugRSLSwfs="true" verifyDigests="true" warn="true">
<compiler additionalCompilerArguments="-locale en_US -define CONFIG::flex4 true" autoRSLOrdering="true" copyDependentFiles="false" flex3CompatMode="true" generateAccessible="false" htmlExpressInstall="true" htmlGenerate="false" htmlHistoryManagement="false" htmlPlayerVersionCheck="true" includeNetmonSwc="false" outputFolderPath="bin" sourceFolderPath="src" strict="true" targetPlayerVersion="0.0.0" useApolloConfig="false" useDebugRSLSwfs="true" verifyDigests="true" warn="true">
<compilerSourcePath/>
<libraryPath defaultLinkType="0">
<libraryPathEntry kind="4" path="">
Expand Down
74 changes: 60 additions & 14 deletions src/com/soenkerohde/ga/FGATracker.as
Expand Up @@ -21,9 +21,13 @@
*/
package com.soenkerohde.ga {
import com.google.analytics.GATracker;
import com.soenkerohde.ga.event.TrackActionEvent;
import com.soenkerohde.ga.event.TrackPageEvent;

import flash.display.DisplayObject;
import flash.events.Event;

import mx.core.Application;
import mx.core.FlexGlobals;
import mx.events.FlexEvent;
import mx.logging.ILogger;
Expand All @@ -46,6 +50,11 @@ package com.soenkerohde.ga {
*/
protected var _account:String;

/**
* @private
*/
protected var _unTracked:Array;

/**
* Google Analytics account ID
* @param account
Expand All @@ -56,19 +65,45 @@ package com.soenkerohde.ga {
}

public function FGATracker() {
FlexGlobals.topLevelApplication.addEventListener( FlexEvent.CREATION_COMPLETE, creationCompleteHandler );
if ( CONFIG::flex4 ) {
FlexGlobals.topLevelApplication.addEventListener( FlexEvent.CREATION_COMPLETE, creationCompleteHandler );
} else {
Application.application.addEventListener( FlexEvent.CREATION_COMPLETE, creationCompleteHandler );
}
}

/**
* @private
*/
protected function creationCompleteHandler( event : FlexEvent ) : void {
FlexGlobals.topLevelApplication.removeEventListener( FlexEvent.CREATION_COMPLETE, creationCompleteHandler );
if ( _account != null ) {
var display:DisplayObject = FlexGlobals.topLevelApplication as DisplayObject;
tracker = new GATracker( display, _account );
} else {
if ( _account == null ) {
throw new Error( "Google Analytics account not set." );
} else {

var display:DisplayObject;

if ( CONFIG::flex4 ) {
FlexGlobals.topLevelApplication.removeEventListener( FlexEvent.CREATION_COMPLETE, creationCompleteHandler );
display = FlexGlobals.topLevelApplication as DisplayObject;
} else {
Application.application.removeEventListener( FlexEvent.CREATION_COMPLETE, creationCompleteHandler );
display = Application.application as DisplayObject;
}
tracker = new GATracker( display, _account );

if ( _unTracked != null ) {
for each ( var e : Event in _unTracked ) {
if ( e is TrackPageEvent ) {
trackPage( e as TrackPageEvent );
} else if ( e is TrackActionEvent ) {
trackAction( e as TrackActionEvent );
} else {
throw new Error( "Unknown event type " + e.type );
}
}

_unTracked = null;
}
}
}

Expand All @@ -78,10 +113,16 @@ package com.soenkerohde.ga {
* Mediates @see com.soenkerohde.ga.event.TrackPageEvent TrackPageEvent
* @param page Page tracking id
*/
[Mediate(event="com.soenkerohde.ga.event.TrackPageEvent.PAGE", properties="page")]
public function trackPage( page : String ) : void {
LOG.info( "trackPage " + page );
tracker.trackPageview( page );
[Mediate(event="com.soenkerohde.ga.event.TrackPageEvent.PAGE")]
public function trackPage( event : TrackPageEvent ) : void {

if ( tracker != null ) {
LOG.info( "trackPage " + event.page );
tracker.trackPageview( event.page );
} else {
_unTracked ||= [];
_unTracked.push( event );
}
}

/**
Expand All @@ -93,10 +134,15 @@ package com.soenkerohde.ga {
* @param label
* @param value
*/
[Mediate(event="com.soenkerohde.ga.event.TrackActionEvent.ACTION", properties="category,action,label,value")]
public function trackAction( category : String, action : String, label : String, value : Number ) : void {
LOG.info( "trackAction " + category + ", " + action + ", " + label + ", " + value );
tracker.trackEvent( category, action, label, value );
[Mediate(event="com.soenkerohde.ga.event.TrackActionEvent.ACTION")]
public function trackAction( event : TrackActionEvent ) : void {
if ( tracker != null ) {
LOG.info( "trackAction " + event.category + ", " + event.action + ", " + event.label + ", " + event.value );
tracker.trackEvent( event.category, event.action, event.label, event.value );
} else {
_unTracked ||= [];
_unTracked.push( event );
}
}


Expand Down

0 comments on commit e77efd7

Please sign in to comment.