PageRenderTime 48ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/analytics/src/actionscript/org/flowplayer/analytics/GoogleTracker.as

https://github.com/longlho/flowplayer
ActionScript | 210 lines | 162 code | 32 blank | 16 comment | 19 complexity | b591d71e0dcf39abe7c2c64608dbc228 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause
  1. /*
  2. * This file is part of Flowplayer, http://flowplayer.org
  3. *
  4. * By: Richard Mueller richard@3232design.com, Anssi Piirainen api@iki.fi
  5. * Copyright (c) 2009-2011 Flowplayer Oy
  6. *
  7. * Released under the MIT License:
  8. * http://www.opensource.org/licenses/mit-license.php
  9. */
  10. package org.flowplayer.analytics {
  11. import com.google.analytics.GATracker;
  12. import com.google.analytics.AnalyticsTracker;
  13. import com.google.analytics.log;
  14. import flash.display.Sprite;
  15. import flash.utils.getTimer;
  16. import org.flowplayer.model.Clip;
  17. import org.flowplayer.model.ClipEvent;
  18. import org.flowplayer.model.ClipEventType;
  19. import org.flowplayer.model.Plugin;
  20. import org.flowplayer.model.PluginModel;
  21. import org.flowplayer.model.PluginError;
  22. import org.flowplayer.model.PlayerEvent;
  23. import org.flowplayer.model.Playlist;
  24. import org.flowplayer.util.Log;
  25. import org.flowplayer.util.PropertyBinder;
  26. import org.flowplayer.util.URLUtil;
  27. import org.flowplayer.view.Flowplayer;
  28. public class GoogleTracker extends Sprite implements Plugin {
  29. private var _log:Log = new Log(this);
  30. private var _model:PluginModel;
  31. private var _player:Flowplayer;
  32. private var _config:Config;
  33. private var _tracker:AnalyticsTracker;
  34. private var _startTimeMillis:int;
  35. private var _viewDurationMillis:int = 0;
  36. public function onConfig(model:PluginModel):void {
  37. _model = model;
  38. _config = Config(new PropertyBinder(new Config()).copyProperties(model.config));
  39. }
  40. //#49 add new logging method for the analytics library
  41. LOG::P
  42. private function gaLogOutput(log:String):void
  43. {
  44. _log.debug(log);
  45. }
  46. public function onLoad(player:Flowplayer):void {
  47. _player = player;
  48. //#49 add new logging method for the analytics library
  49. LOG::P {
  50. log.level = log.VERBOSE;
  51. log.output = gaLogOutput;
  52. }
  53. var events:Events = _config.events;
  54. var playlist:Playlist = _player.playlist;
  55. createClipEventTracker(playlist.onStart, events.start, true);
  56. createClipEventTracker(playlist.onStop, events.stop, true);
  57. createClipEventTracker(playlist.onFinish, events.finish, true);
  58. createClipEventTracker(playlist.onPause, events.pause, events.trackPause);
  59. createClipEventTracker(playlist.onResume, events.resume, events.trackResume);
  60. createClipEventTracker(playlist.onSeek, events.seek, events.trackSeek);
  61. createPlayerEventTracker(_player.onMute, events.mute, events.trackMute);
  62. createPlayerEventTracker(_player.onUnmute, events.unmute, events.trackUnmute);
  63. createPlayerEventTracker(_player.onFullscreen, events.fullscreen, events.trackFullscreen);
  64. createPlayerEventTracker(_player.onFullscreenExit, events.fullscreenExit, events.trackFullscreenExit);
  65. // track unload if the clip is playing or paused. If it's stopped or finished, the Stop event has already
  66. // been tracked or the playback has not started at all.
  67. createPlayerEventTracker(_player.onUnload, events.unload, true, function():Boolean { return _player.isPlaying() || _player.isPaused() });
  68. // bind listeners for tracking the total view time
  69. playlist.onStart(startTimeTracking, null, true);
  70. playlist.onResume(startTimeTracking, null, true);
  71. playlist.onStop(stopTimeTracking, null, true);
  72. playlist.onPause(stopTimeTracking, null, true);
  73. playlist.onFinish(stopTimeTracking, null, true);
  74. _model.dispatchOnLoad();
  75. }
  76. private function startTimeTracking(event:ClipEvent):void {
  77. _startTimeMillis = getTimer();
  78. _log.debug("startTimeTracking(), started at " + _startTimeMillis + ", total view time " + _viewDurationMillis + " milliseconds");
  79. }
  80. private function stopTimeTracking(event:ClipEvent = null):void {
  81. _viewDurationMillis += getTimer() - _startTimeMillis;
  82. _log.debug("stopTimeTracking(), total view time " + _viewDurationMillis + " milliseconds");
  83. }
  84. private function createClipEventTracker(eventBinder:Function, eventName:String, doTrack:Boolean):void {
  85. if (! doTrack) return;
  86. eventBinder(
  87. function(event:ClipEvent):void {
  88. doTrackEvent(eventName, isStopping(event), event);
  89. },
  90. function(clip:Clip):Boolean {
  91. return _config.clipTypes.indexOf(clip.typeStr) >= 0;
  92. });
  93. }
  94. private function createPlayerEventTracker(eventBinder:Function, eventName:String, doTrack:Boolean, extraCheck:Function = null):void {
  95. if (!doTrack) return;
  96. eventBinder(
  97. function(event:PlayerEvent):void {
  98. if (extraCheck != null) {
  99. if (! extraCheck()) {
  100. // extra check returns false --> don't track
  101. return;
  102. }
  103. }
  104. doTrackEvent(eventName, true);
  105. });
  106. }
  107. public function get category():String {
  108. if (_player.playlist.current.getCustomProperty("eventCategory"))
  109. return String(_player.playlist.current.getCustomProperty("eventCategory"));
  110. var pageUrl:String = URLUtil.pageUrl;
  111. if (pageUrl) return pageUrl;
  112. _model.dispatchError(PluginError.ERROR, "Unable to get page URL to be used as google analytics event gategory. Specify this in the analytics plugin config.");
  113. return "Unknown";
  114. }
  115. private function instantiateTracker():void {
  116. try {
  117. _log.debug("Creating tracker in " + _config.mode + " mode using " + _config.accountId);
  118. //#49 refactor to latest analytics library changes
  119. switch (_config.mode) {
  120. case "Bridge":
  121. if (!_config.trackingObj) {
  122. _model.dispatchError(PluginError.ERROR, "Google Analytics tracking object name not specified. ");
  123. return;
  124. }
  125. _tracker = new GATracker(this, _config.trackingObj, _config.mode);
  126. break;
  127. default:
  128. if (!_config.accountId) {
  129. _model.dispatchError(PluginError.ERROR, "Google Analytics account ID not specified. Look it up in your Analytics account, the format is 'UA-XXXXXX-N'");
  130. return;
  131. }
  132. _tracker = new GATracker(this, _config.accountId, _config.mode);
  133. break;
  134. }
  135. } catch(e:Error) {
  136. _model.dispatchError(PluginError.ERROR, "Unable to create tracker: " + e);
  137. }
  138. }
  139. private function doTrackEvent(eventName:String, trackViewDuration:Boolean = false, event:ClipEvent = null):void {
  140. if (_tracker == null) {
  141. instantiateTracker();
  142. }
  143. var clip:Clip = Clip(event ? event.target : _player.currentClip);
  144. try {
  145. var time:int = trackViewDuration ? (_viewDurationMillis / 1000) : int(_player.status ? _player.status.time : 0);
  146. _log.debug("Tracking " + eventName + "[" + (clip.completeUrl + (clip.isInStream ? ": instream" : "")) + "] : " + time + " on page " + category);
  147. if (_tracker.isReady()) {
  148. _tracker.trackEvent(category, eventName, clip.completeUrl + (clip.isInStream ? ": instream" : ""), time);
  149. }
  150. } catch (e:Error) {
  151. _log.error("Got error while tracking event " + eventName);
  152. }
  153. }
  154. private function isStopping(event:ClipEvent):Boolean {
  155. if (event.eventType == ClipEventType.STOP) return true;
  156. if (event.eventType == ClipEventType.FINISH) return true;
  157. return false;
  158. }
  159. [External]
  160. public function trackEvent(eventName:String):void {
  161. doTrackEvent(eventName);
  162. }
  163. [External]
  164. public function setEventName(oldName:String, newName:Object):void {
  165. var newProp:Object = {};
  166. newProp[oldName] = newName == "false" ? null : newName;
  167. _log.debug("setEventName()", newProp);
  168. new PropertyBinder(_config.events).copyProperties(newProp, true);
  169. }
  170. [External(convert="true")]
  171. public function get config():Config {
  172. return _config;
  173. }
  174. public function getDefaultConfig():Object {
  175. return null;
  176. }
  177. }
  178. }