Skip to content

Commit

Permalink
Fix to support complex types in Android version for GoogleTagManager.…
Browse files Browse the repository at this point in the history
…pushDataLayerEvent (aksonov#91)

* Fix issue around complex types https://github.com/idehub/react-native-google-analytics-bridge/issues/90

* Remove previous commented out method
  • Loading branch information
sebawagner authored and cbrevik committed Dec 3, 2016
1 parent c8bb460 commit 5365c85
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.idehub.GoogleAnalyticsBridge;

import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableType;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMapKeySetIterator;

import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;

/**
* Converts a ReadableMap to a HashMap<String, Object>
*/
public class ConvertReadableToMap {

public static HashMap<String, Object> getMap(ReadableMap dictionary) {
HashMap<String, Object> parsedMap = new HashMap<String, Object>();
ReadableMapKeySetIterator iterator = dictionary.keySetIterator();
while (iterator.hasNextKey()) {
String key = iterator.nextKey();
ReadableType type = dictionary.getType(key);
switch (type) {
case Null:
parsedMap.put(key, null);
break;
case Boolean:
parsedMap.put(key, dictionary.getBoolean(key));
break;
case Number:
parsedMap.put(key, dictionary.getDouble(key));
break;
case String:
parsedMap.put(key, dictionary.getString(key));
break;
case Map:
parsedMap.put(key, ConvertReadableToMap.getMap(dictionary.getMap(key)));
break;
case Array:
parsedMap.put(key, ConvertReadableToMap.getArray(dictionary.getArray(key)));
break;
default:
throw new IllegalArgumentException("Could not convert of type: " + type + " and key: " + key + ".");
}
}
return parsedMap;
}

private static List<Object> getArray(ReadableArray readableArray) {
List<Object> parsedList = new ArrayList<Object>(readableArray.size());
for (int i = 0; i < readableArray.size(); i++) {
ReadableType type = readableArray.getType(i);
switch(type) {
case Null:
parsedList.add(i, null);
break;
case Boolean:
parsedList.add(i, readableArray.getBoolean(i));
break;
case Number:
parsedList.add(i, readableArray.getDouble(i));
break;
case String:
parsedList.add(i, readableArray.getString(i));
break;
case Map:
parsedList.add(i, ConvertReadableToMap.getMap(readableArray.getMap(i)));
break;
case Array:
parsedList.add(i, ConvertReadableToMap.getArray(readableArray.getArray(i)));
break;
default:
throw new IllegalArgumentException("Could not convert of type: " + type + " and index: " + i + ".");
}
}
return parsedList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void doubleForKey(final String key, final Promise promise){
public void pushDataLayerEvent(ReadableMap dictionary, final Promise promise){

if (mContainerHolder != null && isValidMapToPushEvent(dictionary)) {
getDataLayer().push(getMap(dictionary));
getDataLayer().push(ConvertReadableToMap.getMap(dictionary));
promise.resolve(true);
} else {
if (mContainerHolder == null) {
Expand All @@ -115,22 +115,14 @@ private boolean isValidMapToPushEvent(ReadableMap dictionary) {
&& 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;
}



}

0 comments on commit 5365c85

Please sign in to comment.