From bfaac4d0c2bf494c10552c24f71f371c84131737 Mon Sep 17 00:00:00 2001 From: karolk Date: Wed, 26 Nov 2014 16:17:58 +0000 Subject: [PATCH 1/4] do not rely on initialized flag, which is now deprecated --- angular-segmentio.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/angular-segmentio.js b/angular-segmentio.js index 5b3b34d..be6b921 100644 --- a/angular-segmentio.js +++ b/angular-segmentio.js @@ -3,7 +3,9 @@ angular.module('segmentio', ['ng']) function($rootScope, $window, $location, $log) { var service = {}; - $window.analytics = $window.analytics || []; + //create a temp queue for events fired before analytics loaded + //but do not attempt to create 'analytics' object + var tempQueue = []; // Define a factory that generates wrapper methods to push arrays of // arguments onto our `analytics` queue, where the first element of the arrays @@ -12,12 +14,18 @@ angular.module('segmentio', ['ng']) return function() { var args = Array.prototype.slice.call(arguments, 0); $log.debug('Call segmentio API with', type, args); - if ($window.analytics.initialized) { + //because we don't overwrite the analytics object we can use + //its presence as a flag that segment.io has loaded + if ($window.analytics) { + if (tempQueue.length) { + $window.analytics.push.apply($window.analytics, tempQueue) + tempQueue.length = 0 + } $log.debug('Segmentio API initialized, calling API'); $window.analytics[type].apply($window.analytics, args); } else { $log.debug('Segmentio API not yet initialized, queueing call'); - $window.analytics.push([type].concat(args)); + tempQueue.push([type].concat(args)); } }; }; From db9ce5b239ef650e2ac17e7d69089ac4da3c81de Mon Sep 17 00:00:00 2001 From: karolk Date: Wed, 26 Nov 2014 16:41:36 +0000 Subject: [PATCH 2/4] install grammar --- angular-segmentio.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/angular-segmentio.js b/angular-segmentio.js index be6b921..32abd2f 100644 --- a/angular-segmentio.js +++ b/angular-segmentio.js @@ -3,8 +3,8 @@ angular.module('segmentio', ['ng']) function($rootScope, $window, $location, $log) { var service = {}; - //create a temp queue for events fired before analytics loaded - //but do not attempt to create 'analytics' object + //Create a temp queue for events fired before analytics loaded + //but do not attempt to create 'analytics' object. var tempQueue = []; // Define a factory that generates wrapper methods to push arrays of @@ -14,8 +14,8 @@ angular.module('segmentio', ['ng']) return function() { var args = Array.prototype.slice.call(arguments, 0); $log.debug('Call segmentio API with', type, args); - //because we don't overwrite the analytics object we can use - //its presence as a flag that segment.io has loaded + //Because we didn't overwrite the analytics object we can use + //its presence as a flag that segment.io has loaded. if ($window.analytics) { if (tempQueue.length) { $window.analytics.push.apply($window.analytics, tempQueue) From bdb5b058f02fe1fc004bbc17039c62055d5b700c Mon Sep 17 00:00:00 2001 From: Vince Date: Wed, 22 Jul 2015 16:15:16 +0100 Subject: [PATCH 3/4] flush the queue of pending events when analytics script is loaded. This avoid keeping events stuck in the queue until the next segmentio method call --- angular-segmentio.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/angular-segmentio.js b/angular-segmentio.js index 32abd2f..582510f 100644 --- a/angular-segmentio.js +++ b/angular-segmentio.js @@ -7,6 +7,20 @@ angular.module('segmentio', ['ng']) //but do not attempt to create 'analytics' object. var tempQueue = []; + + function flushTempQueue () { + // Send the queue of pending events + $window.analytics.push.apply($window.analytics, tempQueue) + tempQueue.length = 0 + } + + // Analytics script is loaded callback. + var analyticsLoaded = function() { + if (tempQueue.length) { + flushTempQueue() + } + } + // Define a factory that generates wrapper methods to push arrays of // arguments onto our `analytics` queue, where the first element of the arrays // is always the name of the analytics.js method itself (eg. `track`). @@ -14,13 +28,10 @@ angular.module('segmentio', ['ng']) return function() { var args = Array.prototype.slice.call(arguments, 0); $log.debug('Call segmentio API with', type, args); + //Because we didn't overwrite the analytics object we can use //its presence as a flag that segment.io has loaded. if ($window.analytics) { - if (tempQueue.length) { - $window.analytics.push.apply($window.analytics, tempQueue) - tempQueue.length = 0 - } $log.debug('Segmentio API initialized, calling API'); $window.analytics[type].apply($window.analytics, args); } else { @@ -58,6 +69,7 @@ angular.module('segmentio', ['ng']) script.async = true; script.src = ('https:' === document.location.protocol ? 'https://' : 'http://') + 'd2dq2ahtl5zl1z.cloudfront.net/analytics.js/v1/' + apiKey + '/analytics.js'; + script.onload = analyticsLoaded // Find the first script element on the page and insert our script next to it. var firstScript = document.getElementsByTagName('script')[0]; From a6ec83cec22928804b1ee2314234831249c35ee5 Mon Sep 17 00:00:00 2001 From: Vince Date: Wed, 22 Jul 2015 18:25:07 +0100 Subject: [PATCH 4/4] move if condition --- angular-segmentio.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/angular-segmentio.js b/angular-segmentio.js index 582510f..1373e88 100644 --- a/angular-segmentio.js +++ b/angular-segmentio.js @@ -3,22 +3,22 @@ angular.module('segmentio', ['ng']) function($rootScope, $window, $location, $log) { var service = {}; - //Create a temp queue for events fired before analytics loaded - //but do not attempt to create 'analytics' object. + // Create a temp queue for events fired before analytics loaded + // but do not attempt to create 'analytics' object. var tempQueue = []; function flushTempQueue () { - // Send the queue of pending events - $window.analytics.push.apply($window.analytics, tempQueue) - tempQueue.length = 0 + if (tempQueue.length) { + // Send the queue of pending events + $window.analytics.push.apply($window.analytics, tempQueue) + tempQueue.length = 0 + } } // Analytics script is loaded callback. var analyticsLoaded = function() { - if (tempQueue.length) { - flushTempQueue() - } + flushTempQueue() } // Define a factory that generates wrapper methods to push arrays of