Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Analytics support #54

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions plugin.xml
Expand Up @@ -87,6 +87,7 @@
<!-- FIREBASE LIBS -->
<header-file src="src/ios/firebase/Firebase.h" />
<framework src="src/ios/firebase/FirebaseAnalytics.framework" custom="true" />
<framework src="src/ios/firebase/FirebaseRemoteConfig.framework" custom="true" />
<framework src="src/ios/firebase/FirebaseInstanceID.framework" custom="true" />
<framework src="src/ios/firebase/FirebaseMessaging.framework" custom="true" />
<framework src="src/ios/firebase/GoogleInterchangeUtilities.framework" custom="true" />
Expand Down
1 change: 1 addition & 0 deletions src/android/FCMPlugin.gradle
Expand Up @@ -15,6 +15,7 @@ repositories {
dependencies {
compile 'com.google.firebase:firebase-core:9.2.0'
compile 'com.google.firebase:firebase-messaging:9.2.0'
compile 'com.google.firebase:firebase-config:9.2.0'
}

// apply plugin: 'com.google.gms.google-services'
Expand Down
113 changes: 112 additions & 1 deletion src/android/FCMPlugin.java
@@ -1,4 +1,5 @@
package com.gae.scaffolder.plugin;
import android.content.Context;

import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CallbackContext;
Expand All @@ -13,12 +14,26 @@
import android.os.Bundle;

import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.analytics.FirebaseAnalytics;

import com.google.firebase.remoteconfig.FirebaseRemoteConfig;
import com.google.firebase.remoteconfig.FirebaseRemoteConfigSettings;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import android.support.annotation.NonNull;




import com.google.firebase.iid.FirebaseInstanceId;

import java.util.Map;

public class FCMPlugin extends CordovaPlugin {

private FirebaseAnalytics mFirebaseAnalytics;
private FirebaseRemoteConfig firebaseRemoteConfig;

private static final String TAG = "FCMPlugin";

public static CordovaWebView gWebView;
Expand All @@ -30,8 +45,10 @@ public FCMPlugin() {}

public void initialize(CordovaInterface cordova, CordovaWebView webView) {
super.initialize(cordova, webView);
final Context context = this.cordova.getActivity().getApplicationContext();
gWebView = webView;
Log.d(TAG, "==> FCMPlugin initialize");
mFirebaseAnalytics = FirebaseAnalytics.getInstance(context);
FirebaseMessaging.getInstance().subscribeToTopic("android");
FirebaseMessaging.getInstance().subscribeToTopic("all");
}
Expand Down Expand Up @@ -96,6 +113,100 @@ public void run() {
}
});
}
else if (action.equals("logEvent")) {
final Bundle params = new Bundle();

params.putString(FirebaseAnalytics.Param.CONTENT_TYPE, args.getString(0));
params.putString(FirebaseAnalytics.Param.ITEM_ID, args.getString(1));

cordova.getThreadPool().execute(new Runnable() {
public void run() {
try{
mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.SELECT_CONTENT, params);
callbackContext.success();
}catch(Exception e){
callbackContext.error(e.getMessage());
}
}
});
}

else if (action.equals("setUserId")) {

cordova.getThreadPool().execute(new Runnable() {
public void run() {
try{
mFirebaseAnalytics.setUserId(args.getString(0));
callbackContext.success();
}catch(Exception e){
callbackContext.error(e.getMessage());
}
}
});
}

else if (action.equals("setUserProperty")) {

cordova.getThreadPool().execute(new Runnable() {
public void run() {
try{
mFirebaseAnalytics.setUserProperty(args.getString(0),args.getString(1));
callbackContext.success();
}catch(Exception e){
callbackContext.error(e.getMessage());
}
}
});
}
// REMOTE CONFIGURATION //
else if (action.equals("getStringValueForKey")) {
cordova.getActivity().runOnUiThread(new Runnable() {
public void run() {
try{

String configValue = firebaseRemoteConfig.getString(args.getString(0));
callbackContext.success(configValue);

}catch(Exception e){
callbackContext.error(e.getMessage());
}
}
});
}
else if (action.equals("initializeRemoteConfig")) {
cordova.getActivity().runOnUiThread(new Runnable() {
public void run() {
try{

firebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder()
.setDeveloperModeEnabled(true)
.build();

firebaseRemoteConfig.fetch(600)
.addOnSuccessListener(
new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
firebaseRemoteConfig.activateFetched();
}
}
)
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
callbackContext.error(e.getMessage());
}
}
);

}catch(Exception e){
callbackContext.error(e.getMessage());
}
}
});
}
else{
callbackContext.error("Method not found");
return false;
Expand Down Expand Up @@ -143,4 +254,4 @@ public static void sendPushPayload(Map<String, Object> payload) {
lastPush = payload;
}
}
}
}
11 changes: 10 additions & 1 deletion src/ios/FCMPlugin.h
@@ -1,12 +1,18 @@
#import <UIKit/UIKit.h>
#import <Cordova/CDVPlugin.h>
#import "Firebase.h"



@interface FCMPlugin : CDVPlugin
{
//NSString *notificationCallBack;
}
@property (nonatomic, strong) FIRRemoteConfig *config;

+ (FCMPlugin *) fcmPlugin;
- (void)initializeRemoteConfig:(CDVInvokedUrlCommand *)command;
- (void)getStringValueForKey:(CDVInvokedUrlCommand *)command;
- (void)ready:(CDVInvokedUrlCommand*)command;
- (void)getToken:(CDVInvokedUrlCommand*)command;
- (void)subscribeToTopic:(CDVInvokedUrlCommand*)command;
Expand All @@ -15,5 +21,8 @@
- (void)notifyOfMessage:(NSData*) payload;
- (void)appEnterBackground;
- (void)appEnterForeground;
- (void)logEvent:(CDVInvokedUrlCommand*)command;
- (void)setUserId:(CDVInvokedUrlCommand *)command;
- (void)setUserProperty:(CDVInvokedUrlCommand *)command;

@end
@end
93 changes: 92 additions & 1 deletion src/ios/FCMPlugin.m
Expand Up @@ -5,7 +5,12 @@

#import <Cordova/CDV.h>
#import "FCMPlugin.h"
#import "Firebase.h"

@import FirebaseInstanceID;
@import FirebaseMessaging;
@import FirebaseAnalytics;



@interface FCMPlugin () {}
@end
Expand All @@ -23,6 +28,52 @@ + (FCMPlugin *) fcmPlugin {
return fcmPluginInstance;
}


- (void)initializeRemoteConfig:(CDVInvokedUrlCommand *)command
{
[self.commandDelegate runInBackground:^{

self.config = [FIRRemoteConfig remoteConfig];

FIRRemoteConfigSettings *remoteConfigSettings = [[FIRRemoteConfigSettings alloc] initWithDeveloperModeEnabled:YES];
self.config.configSettings = remoteConfigSettings;

[self.config fetchWithExpirationDuration:600 completionHandler:^(FIRRemoteConfigFetchStatus status, NSError * _Nullable error) {
[self.config activateFetched];
}];
CDVPluginResult* pluginResult = nil;
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}

-(void)getStringValueForKey:(CDVInvokedUrlCommand *)command
{
NSLog(@"Cordova view ready");
fcmPluginInstance = self;
[self.commandDelegate runInBackground:^{

NSString* propertyString = [command.arguments objectAtIndex:0];

FIRRemoteConfigValue *value = self.config[propertyString];
if(value != nil)
{
NSString *b = value.stringValue;
NSLog(@"remote config val = %@",b);
CDVPluginResult* pluginResult = nil;
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:[NSString stringWithString:b]];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
else
{
CDVPluginResult* pluginResult = nil;
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
}];
}


- (void) ready:(CDVInvokedUrlCommand *)command
{
NSLog(@"Cordova view ready");
Expand All @@ -36,6 +87,46 @@ - (void) ready:(CDVInvokedUrlCommand *)command

}

//Analytics
- (void)logEvent:(CDVInvokedUrlCommand *)command {
[self.commandDelegate runInBackground:^{
NSString* key = [command.arguments objectAtIndex:0];
NSString* value = [command.arguments objectAtIndex:1];

[FIRAnalytics logEventWithName:kFIREventSelectContent parameters:@{
kFIRParameterContentType:key,
kFIRParameterItemID:value
}];

CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}

-(void)setUserId:(CDVInvokedUrlCommand *)command {
[self.commandDelegate runInBackground:^{
NSString *userId = [command.arguments objectAtIndex:0];

[FIRAnalytics setUserID:userId];

CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}

- (void)setUserProperty:(CDVInvokedUrlCommand *)command {
[self.commandDelegate runInBackground:^{
NSString* propertyString = [command.arguments objectAtIndex:0];
NSString* propertyName = [command.arguments objectAtIndex:1];

[FIRAnalytics setUserPropertyString:propertyString forName:propertyName];

CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}


// GET TOKEN //
- (void) getToken:(CDVInvokedUrlCommand *)command
{
Expand Down
Binary file not shown.