PageRenderTime 53ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/google/analytics/components/FlashTracker.as

http://gaforflash.googlecode.com/
ActionScript | 417 lines | 242 code | 68 blank | 107 comment | 13 complexity | a9736891090b6434fc91d21d0f95ccea MD5 | raw file
Possible License(s): Apache-2.0, MPL-2.0-no-copyleft-exception
  1. /*
  2. * Copyright 2008 Adobe Systems Inc., 2008 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. * Contributor(s):
  17. * Zwetan Kjukov <zwetan@gmail.com>.
  18. * Marc Alcaraz <ekameleon@gmail.com>.
  19. */
  20. package com.google.analytics.components
  21. {
  22. import com.google.analytics.API;
  23. import com.google.analytics.AnalyticsTracker;
  24. import com.google.analytics.core.Buffer;
  25. import com.google.analytics.core.Ecommerce;
  26. import com.google.analytics.core.EventTracker;
  27. import com.google.analytics.core.GIFRequest;
  28. import com.google.analytics.core.IdleTimer;
  29. import com.google.analytics.core.ServerOperationMode;
  30. import com.google.analytics.core.TrackerCache;
  31. import com.google.analytics.core.TrackerMode;
  32. import com.google.analytics.core.ga_internal;
  33. import com.google.analytics.debug.DebugConfiguration;
  34. import com.google.analytics.debug.Layout;
  35. import com.google.analytics.events.AnalyticsEvent;
  36. import com.google.analytics.external.AdSenseGlobals;
  37. import com.google.analytics.external.HTMLDOM;
  38. import com.google.analytics.external.JavascriptProxy;
  39. import com.google.analytics.utils.Environment;
  40. import com.google.analytics.utils.Version;
  41. import com.google.analytics.v4.Bridge;
  42. import com.google.analytics.v4.Configuration;
  43. import com.google.analytics.v4.GoogleAnalyticsAPI;
  44. import com.google.analytics.v4.Tracker;
  45. import flash.display.DisplayObject;
  46. import flash.display.Graphics;
  47. import flash.display.MovieClip;
  48. import flash.display.Sprite;
  49. import flash.events.Event;
  50. import flash.utils.getDefinitionByName;
  51. import flash.utils.getQualifiedClassName;
  52. /* force import for type in the includes */
  53. EventTracker;
  54. ServerOperationMode;
  55. /**
  56. * Dispatched after the factory has built the tracker object.
  57. * @eventType com.google.analytics.events.AnalyticsEvent.READY
  58. */
  59. [Event(name="ready", type="com.google.analytics.events.AnalyticsEvent")]
  60. /**
  61. * The Flash visual component.
  62. * You should not instantiate this class by code,
  63. * it's possible but tricky, if you need a code-only component
  64. * use the GATracker class.
  65. */
  66. [IconFile("analytics.png")]
  67. public class FlashTracker extends Sprite implements AnalyticsTracker
  68. {
  69. private var _ready:Boolean = false;
  70. private var _display:DisplayObject;
  71. private var _tracker:GoogleAnalyticsAPI;
  72. //factory
  73. private var _config:Configuration;
  74. private var _debug:DebugConfiguration;
  75. private var _env:Environment;
  76. private var _buffer:Buffer;
  77. private var _gifRequest:GIFRequest;
  78. private var _jsproxy:JavascriptProxy;
  79. private var _dom:HTMLDOM;
  80. private var _adSense:AdSenseGlobals;
  81. private var _idleTimer:IdleTimer;
  82. private var _ecom:Ecommerce;
  83. //component properties
  84. private var _account:String = "";
  85. private var _mode:String = TrackerMode.AS3;
  86. private var _visualDebug:Boolean = false;
  87. //component
  88. protected var preview:MovieClip;
  89. protected var isLivePreview:Boolean;
  90. protected var livePreviewWidth:Number;
  91. protected var livePreviewHeight:Number;
  92. protected var _width:Number = 18;
  93. protected var _height:Number = 18;
  94. protected var _componentInspectorSetting:Boolean;
  95. public var boundingBox_mc:DisplayObject;
  96. public static var version:Version = API.version;
  97. [IconFile("analytics.png")]
  98. public function FlashTracker()
  99. {
  100. super();
  101. _tracker = new TrackerCache();
  102. isLivePreview = _checkLivePreview();
  103. _componentInspectorSetting = false;
  104. if( boundingBox_mc )
  105. {
  106. boundingBox_mc.visible = false;
  107. removeChild( boundingBox_mc );
  108. boundingBox_mc = null;
  109. }
  110. if( isLivePreview )
  111. {
  112. _createLivePreview();
  113. }
  114. /* note:
  115. we have to use the ENTER_FRAME event
  116. to wait 1 frame so we can add to the display list
  117. and get the values declared in the component inspector.
  118. */
  119. addEventListener( Event.ENTER_FRAME, _factory );
  120. }
  121. private function _checkLivePreview():Boolean
  122. {
  123. if( parent != null && (getQualifiedClassName(parent) == "fl.livepreview::LivePreviewParent"))
  124. {
  125. return true;
  126. }
  127. return false;
  128. }
  129. private function _createLivePreview():void
  130. {
  131. preview = new MovieClip();
  132. var g:Graphics = preview.graphics;
  133. g.beginFill(0xffffff);
  134. g.moveTo(0, 0);
  135. g.lineTo(0, _width);
  136. g.lineTo(_width, _height);
  137. g.lineTo(_height, 0);
  138. g.lineTo(0, 0);
  139. g.endFill();
  140. /* note:
  141. because the Icon class is declared in the FLA
  142. and the FLA generate it automatically
  143. we need to use reflection to instanciate it
  144. so compc/asdoc does not generate errors
  145. */
  146. var iconClass:Class = getDefinitionByName( "com.google.analytics.components::Icon" ) as Class;
  147. preview.icon_mc = new iconClass();
  148. preview.icon_mc.name = "icon_mc";
  149. preview.addChild( preview.icon_mc );
  150. addChild( preview );
  151. }
  152. public function set componentInspectorSetting( value:Boolean ):void
  153. {
  154. _componentInspectorSetting = value;
  155. }
  156. public function setSize( w:Number, h:Number ):void
  157. {
  158. /* note:
  159. we don't resize the live preview
  160. we want to keep or default component size
  161. defined in the FLA
  162. */
  163. }
  164. private function _createDebugAndConfig():void
  165. {
  166. if( !_debug )
  167. {
  168. this.debug = new DebugConfiguration();
  169. }
  170. if( !_config )
  171. {
  172. this.config = new Configuration( debug );
  173. }
  174. }
  175. /**
  176. * @private
  177. * Factory to build the different trackers
  178. */
  179. private function _factory( event:Event ):void
  180. {
  181. removeEventListener( Event.ENTER_FRAME, _factory );
  182. if( isLivePreview )
  183. {
  184. /* note:
  185. we don't want to init the factory
  186. when we are in live preview
  187. */
  188. return;
  189. }
  190. _display = this;
  191. _createDebugAndConfig();
  192. if( visualDebug )
  193. {
  194. debug.layout = new Layout( debug, _display );
  195. debug.active = visualDebug;
  196. }
  197. _jsproxy = new JavascriptProxy( debug );
  198. var activeTracker:GoogleAnalyticsAPI;
  199. var cache:TrackerCache = _tracker as TrackerCache;
  200. switch( mode )
  201. {
  202. case TrackerMode.BRIDGE :
  203. {
  204. activeTracker = _bridgeFactory();
  205. break;
  206. }
  207. case TrackerMode.AS3 :
  208. default:
  209. {
  210. activeTracker = _trackerFactory();
  211. }
  212. }
  213. if( !cache.isEmpty() )
  214. {
  215. cache.tracker = activeTracker;
  216. cache.flush();
  217. }
  218. _tracker = activeTracker;
  219. _ready = true ;
  220. dispatchEvent( new AnalyticsEvent( AnalyticsEvent.READY, this ) ) ;
  221. }
  222. /**
  223. * Factory method for returning a Tracker object.
  224. * @private
  225. */
  226. private function _trackerFactory():GoogleAnalyticsAPI
  227. {
  228. debug.info( "GATracker (AS3) v" + version +"\naccount: " + account );
  229. _adSense = new AdSenseGlobals( debug );
  230. _dom = new HTMLDOM( debug );
  231. _dom.cacheProperties();
  232. _env = new Environment( "", "", "", debug, _dom );
  233. _buffer = new Buffer( config, debug, false );
  234. _gifRequest = new GIFRequest( config, debug, _buffer, _env );
  235. _idleTimer = new IdleTimer( config, debug, _display, _buffer );
  236. _ecom = new Ecommerce ( _debug );
  237. use namespace ga_internal;
  238. _env.url = _display.stage.loaderInfo.url;
  239. return new Tracker( account, config, debug, _env, _buffer, _gifRequest, _adSense, _ecom );
  240. }
  241. /**
  242. * @private
  243. * Factory method for returning a Bridge object.
  244. *
  245. * @return {GoogleAnalyticsAPI}
  246. */
  247. private function _bridgeFactory():GoogleAnalyticsAPI
  248. {
  249. debug.info( "GATracker (Bridge) v" + version +"\naccount: " + account );
  250. return new Bridge( account, _debug, _jsproxy );
  251. }
  252. /**
  253. * The Urchin Account.
  254. * You have to define this parameter to initialize the tracking.
  255. */
  256. [Inspectable]
  257. public function get account():String
  258. {
  259. return _account ;
  260. }
  261. /**
  262. * @private
  263. */
  264. public function set account(value:String):void
  265. {
  266. _account = value;
  267. }
  268. /**
  269. * The Tracker configuration.
  270. */
  271. public function get config():Configuration
  272. {
  273. if( !_config )
  274. {
  275. _createDebugAndConfig();
  276. }
  277. return _config;
  278. }
  279. /**
  280. * @private
  281. */
  282. public function set config(value:Configuration):void
  283. {
  284. _config = value;
  285. }
  286. /**
  287. * The Tracker debug configuration.
  288. */
  289. public function get debug():DebugConfiguration
  290. {
  291. if( !_debug )
  292. {
  293. _createDebugAndConfig();
  294. }
  295. return _debug;
  296. }
  297. /**
  298. * @private
  299. */
  300. public function set debug(value:DebugConfiguration):void
  301. {
  302. _debug = value;
  303. }
  304. /**
  305. * Indicates if the tracker is ready to use.
  306. */
  307. public function isReady():Boolean
  308. {
  309. return _ready;
  310. }
  311. /**
  312. * The Traker mode.
  313. * You can select two modes:
  314. * - AS3: use AS3 only, no dependency on HTML/JS
  315. * - Bridge: use AS3 bridged to HTML/JS which define ga.js
  316. */
  317. [Inspectable(defaultValue="AS3", enumeration="AS3,Bridge", type="String")]
  318. public function get mode():String
  319. {
  320. return _mode;
  321. }
  322. /**
  323. * @private
  324. */
  325. public function set mode( value:String ):void
  326. {
  327. _mode = value;
  328. }
  329. /**
  330. * Indicates if the tracker use a visual debug.
  331. * If set to true, at compile time you will
  332. * see a visual debug window with different
  333. * informations about the tracking requests and parameters.
  334. */
  335. [Inspectable(defaultValue="false", type="Boolean")]
  336. public function get visualDebug():Boolean
  337. {
  338. return _visualDebug;
  339. }
  340. /**
  341. * @private
  342. */
  343. public function set visualDebug( value:Boolean ):void
  344. {
  345. _visualDebug = value;
  346. }
  347. include "../common.txt"
  348. }
  349. }