Skip to content

Commit

Permalink
Merge pull request #21 from the-dating-lab/master
Browse files Browse the repository at this point in the history
New pushEvent function for robust data layer usage
  • Loading branch information
kraihn committed Jan 19, 2016
2 parents cec4147 + 634f257 commit 322b3b9
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 115 deletions.
34 changes: 34 additions & 0 deletions src/android/CDVTagManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Collections;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;

/**
* This class echoes a string called from JavaScript.
*/
Expand Down Expand Up @@ -106,6 +111,19 @@ public void containerAvailable(Container container) {
} else {
callback.error("trackEvent failed - not initialized");
}
} else if (action.equals("pushEvent")) {
if (inited) {
try {
DataLayer dataLayer = TagManager.getInstance(this.cordova.getActivity().getApplicationContext()).getDataLayer();
dataLayer.push(objectMap(args.getJSONObject(0)));
callback.success("pushEvent: " + dataLayer.toString());
return true;
} catch (final Exception e) {
callback.error(e.getMessage());
}
} else {
callback.error("pushEvent failed - not initialized");
}
} else if (action.equals("trackPage")) {
if (inited) {
try {
Expand Down Expand Up @@ -134,4 +152,20 @@ public void containerAvailable(Container container) {
}
return false;
}

private Map<Object, Object> objectMap(JSONObject o) throws JSONException {
if (o.length() == 0) {
return Collections.<Object, Object>emptyMap();
}
Map<Object, Object> map = new HashMap<Object, Object>(o.length());
Iterator it = o.keys();
Object key;
Object value;
while (it.hasNext()) {
key = it.next();
value = o.has(key.toString()) ? o.get(key.toString()): null;
map.put(key, value);
}
return map;
}
}
1 change: 1 addition & 0 deletions src/ios/CDVTagManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
- (void) containerAvailable:(TAGContainer *)container;
- (void) exitGTM:(CDVInvokedUrlCommand*)command;
- (void) trackEvent:(CDVInvokedUrlCommand*)command;
- (void) pushEvent:(CDVInvokedUrlCommand*)command;
- (void) trackPage:(CDVInvokedUrlCommand*)command;
- (void) dispatch:(CDVInvokedUrlCommand*)command;

Expand Down
16 changes: 16 additions & 0 deletions src/ios/CDVTagManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,22 @@ - (void) trackEvent:(CDVInvokedUrlCommand*)command
{
TAGDataLayer *dataLayer = [TAGManager instance].dataLayer;
[dataLayer push:@{@"event":@"interaction", @"target":category, @"action":eventAction, @"target-properties":eventLabel, @"value":eventValue}];
[self successWithMessage:@"trackEvent" toID:callbackId];
}
else
[self failWithMessage:@"trackEvent failed - not initialized" toID:callbackId withError:nil];
}

- (void) pushEvent:(CDVInvokedUrlCommand*)command
{
NSString *callbackId = command.callbackId;
NSDictionary *eventData = [command.arguments objectAtIndex:0];

if (inited)
{
TAGDataLayer *dataLayer = [TAGManager instance].dataLayer;
[dataLayer push:eventData];
[self successWithMessage:@"pushEvent" toID:callbackId];
}
else
[self failWithMessage:@"trackEvent failed - not initialized" toID:callbackId withError:nil];
Expand Down
237 changes: 122 additions & 115 deletions www/TagManager.js
Original file line number Diff line number Diff line change
@@ -1,128 +1,135 @@
(function () {
var cordovaRef = window.PhoneGap || window.cordova || window.Cordova;
var queue = [];
var runInterval = 1000;
var running = false;
var runner;
var cordovaRef = window.PhoneGap || window.cordova || window.Cordova;
var queue = [];
var runInterval = 1000;
var running = false;
var runner;

function TagManager() {
}
function TagManager() {}

// initialize google analytics with an account ID and the min number of seconds between posting
//
// 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) {
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
});
};
// initialize google analytics with an account ID and the min number of seconds between posting
//
// 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) {
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.
// eventValue = The event value. This parameter may be -1 to indicate no value.
TagManager.prototype.trackEvent = function (success, fail, 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 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.
// eventValue = The event value. This parameter may be -1 to indicate no value.
TagManager.prototype.trackEvent = function (success, fail, 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) {
var timestamp = new Date().getTime();
queue.push({
timestamp: timestamp,
method: 'trackPage',
success: success,
fail: fail,
pageURL: pageURL
});
};
TagManager.prototype.pushEvent = function (success, fail, eventData) {
var timestamp = new Date().getTime();
queue.push({
timestamp: timestamp,
method: 'pushEvent',
success: success,
fail: fail,
eventData: eventData
});
};

// 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
});
};
// log a page view
//
// pageURL = the URL of the page view
TagManager.prototype.trackPage = function (success, fail, pageURL) {
var timestamp = new Date().getTime();
queue.push({
timestamp: timestamp,
method: 'trackPage',
success: success,
fail: fail,
pageURL: pageURL
});
};

// exit the TagManager instance and stop setInterval
TagManager.prototype.exit = function (success, fail) {
var timestamp = new Date().getTime();
queue.push({
timestamp: timestamp,
method: 'exitGTM',
success: success,
fail: fail
});
};
// 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
});
};

if (cordovaRef && cordovaRef.addConstructor) {
cordovaRef.addConstructor(init);
}
else {
init();
}
// exit the TagManager instance and stop setInterval
TagManager.prototype.exit = function (success, fail) {
var timestamp = new Date().getTime();
queue.push({
timestamp: timestamp,
method: 'exitGTM',
success: success,
fail: fail
});
};

function init() {
if (!window.plugins) {
window.plugins = {};
}
if (!window.plugins.TagManager) {
window.plugins.TagManager = new TagManager();
}
}
if (cordovaRef && cordovaRef.addConstructor) {
cordovaRef.addConstructor(init);
} else {
init();
}

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;
}
}
}
function init() {
if (!window.plugins) {
window.plugins = {};
}
if (!window.plugins.TagManager) {
window.plugins.TagManager = new TagManager();
}
}

if (typeof module != 'undefined' && module.exports) {
module.exports = 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 === 'pushEvent') {
cordovaRef.exec(item.success, item.fail, 'TagManager', item.method, [item.eventData]);
} 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;
}
}
}

if (typeof module != 'undefined' && module.exports) {
module.exports = new TagManager();
}
})();
/* End of Temporary Scope. */

0 comments on commit 322b3b9

Please sign in to comment.