Google Analytics for Flex - Support for Flex 4.5

While trying to use Google Analytics Tracking for Adobe Flash it turned out that it does not support Flex 4.5.

It throws the following exception:
ReferenceError: Error #1065: Variable Application is not defined.
       at global/flash.utils::getDefinitionByName()
       at com.google.analytics.components::FlexTracker()[/buRRRn/projects/GAforFlash/GA_AS3/build/tmp/com/google/analytics/components/FlexTracker.as:102]
Looking at the project's issue tracker, we found the unresolved bug (88) with this same exception.
Looking at the code, the solution was quite simple – The original FlexTracker was looking for the mx.core::Application class in order to get a reference to the Application instance:

 

 var appclass:Object = getDefinitionByName( "mx.core::Application" );
 _app = appclass.application;

 

Now, Flex 4.5 no longer uses this class of course; it is now using FlexGlobals.topLevelApplication for getting a reference to the Application instance.
Wrapping the code with a try/catch with supporting Flex 4.5 resulted with the following code which resolved the issue:
var appclass:Object;
try{//Flex 3
    	appclass = getDefinitionByName( "mx.core::Application" );
	_app = appclass.application;
}
catch (e:Error)
{//Flex 4.5
      appclass = getDefinitionByName( "mx.core::FlexGlobals" );
      _app = appclass.topLevelApplication;
}

 
 
Developer