diff --git a/src/android/CDVTagManager.java b/src/android/CDVTagManager.java index 15fec2c..dfc523e 100644 --- a/src/android/CDVTagManager.java +++ b/src/android/CDVTagManager.java @@ -27,6 +27,7 @@ import org.apache.cordova.CordovaPlugin; import org.apache.cordova.CordovaInterface; +import com.google.analytics.tracking.android.GAServiceManager; import com.google.tagmanager.Container; import com.google.tagmanager.ContainerOpener; import com.google.tagmanager.ContainerOpener.OpenType; @@ -56,6 +57,9 @@ public void initialize(CordovaInterface cordova, CordovaWebView webView) { public boolean execute(String action, JSONArray args, CallbackContext callback) { if (action.equals("initGTM")) { try { + // Set the dispatch interval + GAServiceManager.getInstance().setLocalDispatchPeriod(args.getInt(1)); + TagManager tagManager = TagManager.getInstance(this.cordova.getActivity().getApplicationContext()); ContainerOpener.openContainer( tagManager, // TagManager instance. @@ -118,6 +122,18 @@ public void containerAvailable(Container container) { else { callback.error("trackPage failed - not initialized"); } + } else if (action.equals("dispatch")) { + if (inited) { + try { + GAServiceManager.getInstance().dispatchLocalHits(); + } + catch (final Exception e) { + callback.error(e.getMessage()); + } + } + else { + callback.error("dispatch failed - not initialized"); + } } return false; } diff --git a/src/ios/CDVTagManager.h b/src/ios/CDVTagManager.h index b059fb6..244a5f4 100644 --- a/src/ios/CDVTagManager.h +++ b/src/ios/CDVTagManager.h @@ -43,5 +43,6 @@ - (void) exitGTM:(CDVInvokedUrlCommand*)command; - (void) trackEvent:(CDVInvokedUrlCommand*)command; - (void) trackPage:(CDVInvokedUrlCommand*)command; +- (void) dispatch:(CDVInvokedUrlCommand*)command; @end diff --git a/src/ios/CDVTagManager.m b/src/ios/CDVTagManager.m index c80d1dc..b97c4a8 100644 --- a/src/ios/CDVTagManager.m +++ b/src/ios/CDVTagManager.m @@ -98,6 +98,18 @@ - (void) trackPage:(CDVInvokedUrlCommand*)command [self failWithMessage:@"trackPage failed - not initialized" toID:callbackId withError:nil]; } +- (void) dispatch:(CDVInvokedUrlCommand*)command +{ + NSString *callbackId = command.callbackId; + + if (inited) + { + [self.tagManager dispatch]; + } + else + [self failWithMessage:@"dispatch failed - not initialized" toID:callbackId withError:nil]; +} + -(void) successWithMessage:(NSString *)message toID:(NSString *)callbackID { CDVPluginResult *commandResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:message]; diff --git a/www/TagManager.js b/www/TagManager.js index d5ff788..e63b086 100644 --- a/www/TagManager.js +++ b/www/TagManager.js @@ -1,5 +1,9 @@ (function () { var cordovaRef = window.PhoneGap || window.cordova || window.Cordova; + var queue = []; + var runInterval = 1000; + var running = false; + var runner; function TagManager() { } @@ -9,29 +13,73 @@ // id = the GTM account ID of the form 'GTM-000000' // period = the minimum interval for transmitting tracking events if any exist in the queue TagManager.prototype.init = function (success, fail, id, period) { - return cordovaRef.exec(success, fail, 'TagManager', 'initGTM', [id, period]); + runner = setInterval(run, runInterval); + running = true; + var timestamp = new Date().getTime(); + queue.push({ + timestamp: timestamp, + method: 'initGTM', + success: success, + fail: fail, + id: id, + period: period + }); }; // log an event // // category = The event category. This parameter is required to be non-empty. // eventAction = The event action. This parameter is required to be non-empty. - // eventLabel = The event label. This parameter may be a blank string to indicate no label. - // eventLabel = The event label. This parameter may be a blank string to indicate no label. + // eventLabel = The event label. This parameter may be a blank string to indicate no label. // eventValue = The event value. This parameter may be -1 to indicate no value. TagManager.prototype.trackEvent = function (success, fail, category, eventAction, eventLabel, eventValue) { - return cordovaRef.exec(success, fail, 'TagManager', 'trackEvent', [category, eventAction, eventLabel, eventValue]); + var timestamp = new Date().getTime(); + queue.push({ + timestamp: timestamp, + method: 'trackEvent', + success: success, + fail: fail, + category: category, + eventAction: eventAction, + eventLabel: eventLabel, + eventValue: eventValue + }); }; // log a page view // // pageURL = the URL of the page view TagManager.prototype.trackPage = function (success, fail, pageURL) { - return cordovaRef.exec(success, fail, 'TagManager', 'trackPage', [pageURL]); + var timestamp = new Date().getTime(); + queue.push({ + timestamp: timestamp, + method: 'trackPage', + success: success, + fail: fail, + pageURL: pageURL + }); }; + // force a dispatch to Tag Manager + TagManager.prototype.dispatch = function (success, fail) { + var timestamp = new Date().getTime(); + queue.push({ + timestamp: timestamp, + method: 'dispatch', + success: success, + fail: fail + }); + }; + + // exit the TagManager instance and stop setInterval TagManager.prototype.exit = function (success, fail) { - return cordovaRef.exec(success, fail, 'TagManager', 'exitGTM', []); + var timestamp = new Date().getTime(); + queue.push({ + timestamp: timestamp, + method: 'exitGTM', + success: success, + fail: fail + }); }; if (cordovaRef && cordovaRef.addConstructor) { @@ -46,7 +94,30 @@ window.plugins = {}; } if (!window.plugins.TagManager) { - window.plugins.TagManager = new TagManager(); + window.plugins.TagManager = new TagManager(); + } + } + + function run() { + if (queue.length > 0) { + var item = queue.shift(); + if (item.method === 'initGTM') { + cordovaRef.exec(item.success, item.fail, 'TagManager', item.method, [item.id, item.period]); + } + else if (item.method === 'trackEvent') { + cordovaRef.exec(item.success, item.fail, 'TagManager', item.method, [item.category, item.eventAction, item.eventLabel, item.eventValue]); + } + else if (item.method === 'trackPage') { + cordovaRef.exec(item.success, item.fail, 'TagManager', item.method, [item.pageURL]); + } + else if (item.method === 'dispatch') { + cordovaRef.exec(item.success, item.fail, 'TagManager', item.method, []); + } + else if (item.method === 'exitGTM') { + cordovaRef.exec(item.success, item.fail, 'TagManager', item.method, []); + clearInterval(runner); + running = false; + } } }