Skip to content

Commit

Permalink
Merge branch 'master' of github.com:kraihn/cordova-plugin-tag-manager
Browse files Browse the repository at this point in the history
  • Loading branch information
kraihn committed Jan 6, 2015
2 parents dd0379b + bc7017d commit 0becdb8
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 7 deletions.
16 changes: 16 additions & 0 deletions src/android/CDVTagManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions src/ios/CDVTagManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@
- (void) exitGTM:(CDVInvokedUrlCommand*)command;
- (void) trackEvent:(CDVInvokedUrlCommand*)command;
- (void) trackPage:(CDVInvokedUrlCommand*)command;
- (void) dispatch:(CDVInvokedUrlCommand*)command;

@end
12 changes: 12 additions & 0 deletions src/ios/CDVTagManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
85 changes: 78 additions & 7 deletions www/TagManager.js
Original file line number Diff line number Diff line change
@@ -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() {
}
Expand All @@ -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) {
Expand All @@ -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;
}
}
}

Expand Down

0 comments on commit 0becdb8

Please sign in to comment.