Skip to content

Commit

Permalink
Merge pull request aksonov#60 from rdesai40/master
Browse files Browse the repository at this point in the history
implemented Google Tag Manager Datalayer api to push event
  • Loading branch information
cbrevik authored Sep 4, 2016
2 parents 8c89f52 + 8d44db9 commit 855d87f
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableMapKeySetIterator;
import com.facebook.stetho.common.StringUtil;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.tagmanager.ContainerHolder;
import com.google.android.gms.tagmanager.DataLayer;
import com.google.android.gms.tagmanager.TagManager;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class GoogleTagManagerBridge extends ReactContextBaseJavaModule {
Expand All @@ -22,9 +28,11 @@ public GoogleTagManagerBridge(ReactApplicationContext reactContext) {
private final String E_ONGOING_OPEN_OPERATION = "E_ONGOING_OPEN_OPERATION";
private final String E_CONTAINER_NOT_OPENED = "E_CONTAINER_NOT_OPENED";
private final String E_OPEN_CONTAINER_FAILED = "E_OPEN_CONTAINER_FAILED";
private final String E_PUSH_EVENT_FAILED = "E_PUSH_EVENT_FAILED";

private ContainerHolder mContainerHolder;
private Boolean openOperationInProgress = false;
private DataLayer mDatalayer;

@Override
public String getName() {
Expand Down Expand Up @@ -87,4 +95,43 @@ public void doubleForKey(final String key, final Promise promise){
promise.reject(E_CONTAINER_NOT_OPENED, new Throwable("The container has not been opened. You must call openContainerWithId(..)"));
}
}
}

@ReactMethod
public void pushDataLayerEvent(ReadableMap dictionary, final Promise promise){

if (mContainerHolder != null && isValidMapToPushEvent(dictionary)) {
getDataLayer().push(getMap(dictionary));
promise.resolve(true);
} else {
if (mContainerHolder == null) {
promise.reject(E_CONTAINER_NOT_OPENED, new Throwable("The container has not been opened. You must call openContainerWithId(..)"));
} else {
promise.reject(E_PUSH_EVENT_FAILED, new Throwable("Validation error, data must have a key \"event\" with valid event name"));
}
}
}

private boolean isValidMapToPushEvent(ReadableMap dictionary) {
return (dictionary != null && dictionary.getString("event") != null
&& dictionary.getString("event").length() > 0);
}

private Map<String,Object> getMap(ReadableMap dictionary) {
Map<String, Object> map = new HashMap<>();
ReadableMapKeySetIterator iterator = dictionary.keySetIterator();
while (iterator.hasNextKey()) {
String key = iterator.nextKey();
String value = dictionary.getString(key);
map.put(key, value);
}
return map;
}

private DataLayer getDataLayer() {
if (mDatalayer == null) {
TagManager tagManager = TagManager.getInstance(getReactApplicationContext());
mDatalayer = tagManager.getDataLayer();
}
return mDatalayer;
}
}
11 changes: 11 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,17 @@ class GoogleTagManager {
static doubleForKey(key){
return GoogleTagManagerBridge.doubleForKey(key);
}

/**
* push a datalayer event for Google Analytics through Google Tag Manager.
* @param {String} eventName
* @param {Object} dictionaly An Map<String, Object> containing key and value pairs.
it must have atleast one key "event" with event name
* example: {event: "eventName", pageId: "/home"}
*/
static pushDataLayerEvent(dictionaly = {}){
GoogleTagManagerBridge.pushDataLayerEvent(dictionaly);
}
}

GoogleAnalytics.GoogleTagManager = GoogleTagManager;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#import "RCTGoogleTagManagerBridge.h"
#import "TAGContainer.h"
#import "TagContainerOpener.h"
#import "TAGDataLayer.h"

@interface RCTGoogleTagManagerBridge ()<TAGContainerOpenerNotifier>
@end

@implementation RCTGoogleTagManagerBridge {

}

RCT_EXPORT_MODULE();
Expand All @@ -16,6 +17,7 @@ @implementation RCTGoogleTagManagerBridge {
NSString *const E_CONTAINER_ALREADY_OPEN = @"E_CONTAINER_ALREADY_OPEN";
NSString *const E_ONGOING_OPEN_OPERATION = @"E_ONGOING_OPEN_OPERATION";
NSString *const E_CONTAINER_NOT_OPENED = @"E_CONTAINER_NOT_OPENED";
NSString *const E_PUSH_EVENT_FAILED = @"E_PUSH_EVENT_FAILED";


RCT_EXPORT_METHOD(openContainerWithId:(NSString *)containerId
Expand All @@ -26,18 +28,18 @@ @implementation RCTGoogleTagManagerBridge {
reject(E_CONTAINER_ALREADY_OPEN, nil, RCTErrorWithMessage(@"The container is already open."));
return;
}

if (self.openContainerResolver) {
reject(E_ONGOING_OPEN_OPERATION, nil, RCTErrorWithMessage(@"Container open-operation already in progress."));
return;
}

if (self.tagManager == nil) {
self.tagManager = [TAGManager instance];
}

self.openContainerResolver = resolve;

[TAGContainerOpener openContainerWithId:containerId
tagManager:self.tagManager
openType:kTAGOpenTypePreferFresh
Expand Down Expand Up @@ -76,6 +78,22 @@ @implementation RCTGoogleTagManagerBridge {
}
}

RCT_EXPORT_METHOD(pushDataLayerEvent:(NSDictionary*)dictionary
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject){
if (self.container != nil && [[dictionary allKeys] containsObject:@"event"]) {
[[TAGManager instance].dataLayer push:dictionary];
[[TAGManager instance] dispatch];
resolve(@YES);
} else {
if (self.container == nil) {
reject(E_CONTAINER_NOT_OPENED, nil, RCTErrorWithMessage(@"The container has not been opened. You must call openContainerWithId(..)"));
} else {
reject(E_PUSH_EVENT_FAILED, nil, RCTErrorWithMessage(@"Validation error, data must have a key \"event\" with valid event name"));
}
}
}

- (void)containerAvailable:(TAGContainer *)container {
dispatch_async(_methodQueue, ^{
self.container = container;
Expand All @@ -84,4 +102,4 @@ - (void)containerAvailable:(TAGContainer *)container {
});
}

@end
@end

0 comments on commit 855d87f

Please sign in to comment.