Skip to content

Instantly share code, notes, and snippets.

@cosmith
Created December 16, 2015 22:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cosmith/5fd26d0125767d88f288 to your computer and use it in GitHub Desktop.
Save cosmith/5fd26d0125767d88f288 to your computer and use it in GitHub Desktop.
React Native Flurry
"use strict";
/**
* @providesModule Flurry
*/
var FlurryManager = require("react-native").NativeModules.FlurryManager;
var Log = require("Log");
class Flurry {
static logEvent(event) {
Log.debug("[Flurry]", event);
if (!__DEV__) {
FlurryManager.logEvent(event);
}
}
static logEventWithParameters(event, params) {
Log.debug("[Flurry]", event, params);
if (!__DEV__) {
FlurryManager.logEventWithParameters(event, params);
}
}
static logError(error, isFatal) {
var details = `at line: ${error.line}\nstack: ${error.stack}\nfatal: ${isFatal}`;
if (!__DEV__) {
FlurryManager.logError(error.message, details);
}
}
}
module.exports = Flurry;
package com.truckfly.rctflurry;
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.flurry.android.FlurryAgent;
import java.util.HashMap;
public class FlurryModule extends ReactContextBaseJavaModule {
public FlurryModule(ReactApplicationContext reactContext) {
super(reactContext);
}
@Override
public String getName() {
return "FlurryManager";
}
@ReactMethod
public void logEvent(String message) {
FlurryAgent.logEvent(message);
}
@ReactMethod
public void logEventWithParameters(
String message,
ReadableMap params) {
HashMap<String, String> map = new HashMap<String, String>();
// /!\ .keySetIterator() creates a new iterator at every call
// so calling it directly in the while() will give an infinite loop
ReadableMapKeySetIterator iterator = params.keySetIterator();
while (iterator.hasNextKey()) {
String key = iterator.nextKey();
String value = params.getString(key);
map.put(key, value);
}
FlurryAgent.logEvent(message, map);
}
@ReactMethod
public void logError(String title, String message) {
FlurryAgent.onError(title, message, "JavaScript error");
}
}
package com.truckfly.rctflurry;
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.JavaScriptModule;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class FlurryPackage implements ReactPackage {
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
modules.add(new FlurryModule(reactContext));
return modules;
}
@Override
public List<Class<? extends JavaScriptModule>> createJSModules() {
return Collections.emptyList();
}
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Arrays.<ViewManager>asList();
}
}
#import "RCTViewManager.h"
@interface RCTFlurryManager : RCTViewManager
@end
#import "RCTFlurryManager.h"
#import "RCTBridge.h"
#import "RCTEventDispatcher.h"
#import "UIView+React.h"
#import "Flurry.h"
@implementation RCTFlurryManager
RCT_EXPORT_MODULE()
RCT_REMAP_METHOD(logEvent, logEvent
: (NSString*)eventName)
{
[Flurry logEvent:eventName];
}
RCT_REMAP_METHOD(logEventWithParameters, logEvent
: (NSString*)eventName withParameters
: (NSDictionary*)params)
{
[Flurry logEvent:eventName withParameters:params];
}
RCT_REMAP_METHOD(setUserId, setUserId
: (NSString*)userId)
{
[Flurry setUserID:userId];
}
RCT_REMAP_METHOD(logError, logError
: (NSString*)title message
: (NSString*)message)
{
[Flurry logError:title message:message error:nil];
NSLog(@"JS ERROR %@ %@", title, message);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment