Skip to content

Commit

Permalink
Analytics: switched from Flox to Google Analytics Measurement Protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
joshtynjala committed Jul 6, 2015
1 parent e28b55c commit 379f81f
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 70 deletions.
12 changes: 4 additions & 8 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@
</and>
</condition>
</fail>
<fail unless="flox.id" message="Must define flox.id in build.local.properties for Flox analytics"/>
<fail unless="flox.key" message="Must define flox.key in build.local.properties for Flox analytics"/>
<fail unless="analytics.id" message="Must define analytics.id in build.local.properties for analytics"/>

<target name="build" depends="-build-win,-build-mac" description="Builds the Feathers SDK Manager for the current platform"/>

Expand Down Expand Up @@ -71,14 +70,11 @@
<arg value="${frameworks.root}"/>
<arg value="+configname=air"/>
<arg value="-define"/>
<arg value="CONFIG::USE_FLOX"/>
<arg value="CONFIG::USE_ANALYTICS"/>
<arg value="true"/>
<arg value="-define"/>
<arg value="CONFIG::FLOX_ID"/>
<arg value="&quot;${flox.id}&quot;"/>
<arg value="-define"/>
<arg value="CONFIG::FLOX_KEY"/>
<arg value="&quot;${flox.key}&quot;"/>
<arg value="CONFIG::ANALYTICS_TRACKING_ID"/>
<arg value="&quot;${analytics.id}&quot;"/>
<arg value="-swf-version=${swf.version}"/>
<arg value="-source-path+=${source.root}"/>
<arg value="-library-path+=${library.root}"/>
Expand Down
22 changes: 11 additions & 11 deletions source/FeathersSDKManagerContext.as
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ limitations under the License.
*/
package
{
import com.gamua.flox.Flox;
import commands.AnalyticsInitCommand;

import commands.FloxInitCommand;
import commands.FloxLogErrorCommand;
import commands.FloxLogEventInstallCompleteCommand;
import commands.AnalyticsErrorCommand;
import commands.AnalyticsEventInstallCompleteCommand;

import events.AcquireProductServiceEventType;
import events.RunInstallScriptServiceEventType;
Expand Down Expand Up @@ -79,12 +78,13 @@ package
var applicationVersion:String = applicationDescriptor.ns::versionNumber.toString();
this.injector.mapValue(String, applicationVersion, "applicationVersion");

CONFIG::USE_FLOX
CONFIG::USE_ANALYTICS
{
this.commandMap.mapEvent(ContextEventType.STARTUP_COMPLETE, FloxInitCommand);
this.commandMap.mapEvent(RunInstallScriptServiceEventType.COMPLETE, FloxLogEventInstallCompleteCommand);
this.commandMap.mapEvent(RunInstallScriptServiceEventType.ERROR, FloxLogErrorCommand);
this.commandMap.mapEvent(AcquireProductServiceEventType.ERROR, FloxLogErrorCommand);
this.commandMap.mapEvent(ContextEventType.STARTUP_COMPLETE, AnalyticsInitCommand);
this.commandMap.mapEvent(RunInstallScriptServiceEventType.COMPLETE, AnalyticsEventInstallCompleteCommand);
this.commandMap.mapEvent(RunInstallScriptServiceEventType.ERROR, AnalyticsErrorCommand);
this.commandMap.mapEvent(AcquireProductServiceEventType.ERROR, AnalyticsErrorCommand);
this.commandMap.mapEvent(UncaughtErrorEvent.UNCAUGHT_ERROR, AnalyticsErrorCommand);
Starling.current.nativeStage.root.loaderInfo.uncaughtErrorEvents.addEventListener(
UncaughtErrorEvent.UNCAUGHT_ERROR, uncaughtErrorEvents_uncaughtErrorEventHandler);
}
Expand All @@ -108,11 +108,11 @@ package
if(error is Error)
{
var errorError:Error = Error(error);
Flox.logError(error, "Uncaught Error: " + errorError.message);
this.dispatchEventWith(UncaughtErrorEvent.UNCAUGHT_ERROR, false, errorError.message);
}
else
{
Flox.logError("UncaughtError", error);
this.dispatchEventWith(UncaughtErrorEvent.UNCAUGHT_ERROR, false, error);
}
}
}
Expand Down
223 changes: 223 additions & 0 deletions source/com/bowlerhatsoftware/analytics/GAMeasurementProtocol.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
/*
Copyright 2015 Bowler Hat LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.bowlerhatsoftware.analytics
{
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;

/**
* A simple implementation of the Google Analytics Measurement Protocol.
*/
public class GAMeasurementProtocol
{
/**
* @private
*/
internal static var loaders:Vector.<URLLoader> = new <URLLoader>[];

/**
* @private
*/
private static var PRODUCTION_URL:String = "https://www.google-analytics.com/collect";

/**
* @private
*/
private static var DEBUG_URL:String = "https://www.google-analytics.com/debug/collect";

/**
* The Google Analytics tracking ID for your property. Has the following
* format: UA-XXXX-Y. Required.
*/
public static var trackingID:String;

/**
* An anonymous client ID. Required.
*/
public static var clientID:String;

/**
* The name of your application.
*/
public static var applicationName:String;

/**
* The version of your application.
*/
public static var applicationVersion:String;

/**
* When <code>true</code>, the data will be sent to the Google Analytics
* Measurement Protocol Validation Server. The result will be displayed
* in the console.
*/
public static var debugMode:Boolean = false;

/**
* Tracks an event.
*/
public static function trackEvent(eventCategory:String, eventAction:String, eventLabel:String = null, eventValue:int = -1):void
{
var parameters:URLVariables = createURLVariablesWithDefaults();
parameters.t = "event";
parameters.ec = eventCategory;
parameters.ea = eventAction;
if(eventLabel !== null && eventLabel.length > 0)
{
parameters.el = eventLabel;
}
if(eventValue >= 0)
{
parameters.ev = eventValue.toString();
}

loadURLRequestWithParameters(parameters);
}

/**
* Tracks an exception.
*/
public static function trackException(exceptionDescription:String, isFatal:Boolean):void
{
var parameters:URLVariables = createURLVariablesWithDefaults();
parameters.t = "exception";
parameters.exd = exceptionDescription;
parameters.exf = isFatal ? "1" : "0";

loadURLRequestWithParameters(parameters);
}

/**
* @private
*/
private static function validateGlobalParameters():void
{
if(GAMeasurementProtocol.trackingID === null || GAMeasurementProtocol.trackingID.length === 0)
{
throw new ArgumentError("MeasurementProtocol.trackingID cannot be null.")
}
if(GAMeasurementProtocol.clientID === null || GAMeasurementProtocol.clientID.length === 0)
{
throw new ArgumentError("MeasurementProtocol.clientID cannot be null.")
}
}

/**
* @private
*/
private static function createURLVariablesWithDefaults():URLVariables
{
validateGlobalParameters();
var parameters:URLVariables = new URLVariables();
parameters.v = "1";
parameters.tid = trackingID;
parameters.cid = clientID;
if(applicationName !== null && applicationName.length > 0)
{
parameters.an = applicationName;
}
if(applicationVersion !== null && applicationVersion.length > 0)
{
parameters.av = applicationVersion;
}
return parameters;
}

/**
* @private
*/
private static function loadURLRequestWithParameters(parameters:URLVariables):void
{
var url:String = debugMode ? DEBUG_URL : PRODUCTION_URL;
var request:URLRequest = new URLRequest(url);
request.method = URLRequestMethod.POST;
request.data = parameters;

var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, loader_completeHandler);
loader.addEventListener(IOErrorEvent.IO_ERROR, loader_ioErrorHandler);
loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, loader_securityErrorHandler);
loaders[loaders.length] = loader;
loader.load(request);
}

/**
* @private
*/
private static function cleanupLoader(loader:URLLoader):void
{
loader.removeEventListener(Event.COMPLETE, loader_completeHandler);
loader.removeEventListener(IOErrorEvent.IO_ERROR, loader_ioErrorHandler);
var index:int = loaders.indexOf(loader);
if(index === 0)
{
loaders.shift();
}
else if(index === loaders.length - 1)
{
loaders.pop();
}
else
{
loaders.splice(index, 1);
}
}

/**
* @private
*/
private static function loader_completeHandler(event:Event):void
{
var loader:URLLoader = URLLoader(event.currentTarget);
cleanupLoader(loader);
if(debugMode)
{
trace(loader.data);
}
}

/**
* @private
*/
private static function loader_ioErrorHandler(event:IOErrorEvent):void
{
var loader:URLLoader = URLLoader(event.currentTarget);
cleanupLoader(loader);
if(debugMode)
{
trace(event);
}
}

/**
* @private
*/
private static function loader_securityErrorHandler(event:SecurityErrorEvent):void
{
var loader:URLLoader = URLLoader(event.currentTarget);
cleanupLoader(loader);
if(debugMode)
{
trace(event);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ limitations under the License.
*/
package commands
{
import com.gamua.flox.Flox;

import model.SDKManagerModel;

import com.bowlerhatsoftware.analytics.GAMeasurementProtocol;
import org.robotlegs.starling.mvcs.Command;

import starling.events.Event;

public class FloxLogErrorCommand extends Command
public class AnalyticsErrorCommand extends Command
{
[Inject]
public var event:Event;
Expand All @@ -34,8 +33,9 @@ package commands

override public function execute():void
{
var message:String = event.data as String;
Flox.logError(this.event.type, message)
var errorType:String = this.event.type;
var message:String = this.event.data as String;
GAMeasurementProtocol.trackException(errorType + ": " + message, true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,22 @@ limitations under the License.
*/
package commands
{
import com.gamua.flox.Flox;

import model.SDKManagerModel;

import com.bowlerhatsoftware.analytics.GAMeasurementProtocol;

import org.robotlegs.starling.mvcs.Command;

public class FloxLogEventInstallCompleteCommand extends Command
public class AnalyticsEventInstallCompleteCommand extends Command
{
private static const EVENT_NAME:String = "InstallComplete";

[Inject]
public var sdkManagerModel:SDKManagerModel;

override public function execute():void
{
var productVersion:String = sdkManagerModel.selectedProduct.versionNumber;
var runtimeVersion:String = sdkManagerModel.selectedRuntime.airVersionNumber;
var operatingSystem:String = sdkManagerModel.operatingSystem;
Flox.logEvent(EVENT_NAME,
{
productVersion: productVersion,
runtimeVersion: runtimeVersion,
operatingSystem: operatingSystem
});
Flox.shutdown();
GAMeasurementProtocol.trackEvent("InstallComplete", operatingSystem, productVersion);
}
}
}
Loading

0 comments on commit 379f81f

Please sign in to comment.