diff --git a/README.md b/README.md index 63cd75019..4137b98f6 100644 --- a/README.md +++ b/README.md @@ -323,3 +323,15 @@ facebookConnectPlugin.showDialog({ } ); ``` + +### Hybrid Mobile App Events + +Starting from Facebook SDK v4.34 for both iOS and Android, there is a new way of converting pixel events into mobile app events. For more information: [https://developers.facebook.com/docs/app-events/hybrid-app-events/](https://developers.facebook.com/docs/app-events/hybrid-app-events/) + +In order to enable this feature in your cordova app, please set the *FACEBOOK_HYBRID_APP_EVENTS* variable to "true"(default is false): +```bash +$ cordova plugin add cordova-plugin-facebook4 --save --variable APP_ID="123456789" --variable APP_NAME="myApplication" --variable FACEBOOK_HYBRID_APP_EVENTS="true" +``` +Please check [this repo](https://github.com/msencer/fb_hybrid_app_events_sample) for an example app using this feature. + +**NOTE(iOS):** This feature only works with WKWebView so until [Cordova iOS 5 is relased](https://cordova.apache.org/news/2018/08/01/future-cordova-ios-webview.html), an additional plugin (e.g cordova-plugin-wkwebview-engine) is needed. diff --git a/plugin.xml b/plugin.xml index 84f1e56d4..4bb571dec 100644 --- a/plugin.xml +++ b/plugin.xml @@ -12,13 +12,14 @@ The Facebook plugin for Apache Cordova allows you to use the same JavaScript code in your Cordova application as you use in your web application. - + https://github.com/jeduan/cordova-plugin-facebook4 Apache 2.0 + @@ -52,11 +53,13 @@ $APP_ID $APP_NAME + $FACEBOOK_HYBRID_APP_EVENTS $APP_ID $APP_NAME + $FACEBOOK_HYBRID_APP_EVENTS @@ -104,6 +107,10 @@ $APP_NAME + + $FACEBOOK_HYBRID_APP_EVENTS + + diff --git a/src/android/ConnectPlugin.java b/src/android/ConnectPlugin.java index 3aa97ce22..04b967e97 100644 --- a/src/android/ConnectPlugin.java +++ b/src/android/ConnectPlugin.java @@ -1,9 +1,12 @@ package org.apache.cordova.facebook; +import android.content.Context; import android.content.Intent; +import android.content.res.Resources; import android.net.Uri; import android.os.Bundle; import android.util.Log; +import android.webkit.WebView; import com.facebook.AccessToken; import com.facebook.CallbackManager; @@ -86,6 +89,9 @@ protected void pluginInitialize() { // create AppEventsLogger logger = AppEventsLogger.newLogger(cordova.getActivity().getApplicationContext()); + // augment web view to enable hybrid app events + enableHybridAppEvents(); + // Set up the activity result callback to this class cordova.setActivityResultCallback(this); @@ -701,6 +707,23 @@ private void executeLogin(JSONArray args, CallbackContext callbackContext) throw } } + private void enableHybridAppEvents() { + try { + Context appContext = cordova.getActivity().getApplicationContext(); + Resources res = appContext.getResources(); + int enableHybridAppEventsId = res.getIdentifier("fb_hybrid_app_events", "bool", appContext.getPackageName()); + boolean enableHybridAppEvents = enableHybridAppEventsId != 0 && res.getBoolean(enableHybridAppEventsId); + if (enableHybridAppEvents) { + AppEventsLogger.augmentWebView((WebView) this.webView.getView(), appContext); + Log.d(TAG, "Hybrid app events are enabled"); + } else { + Log.d(TAG, "Hybrid app events are not enabled"); + } + } catch (Exception e) { + Log.d(TAG, "Hybrid app events cannot be enabled"); + } + } + private ShareLinkContent buildContent(Map paramBundle) { ShareLinkContent.Builder builder = new ShareLinkContent.Builder(); if (paramBundle.containsKey("href")) diff --git a/src/ios/FacebookConnectPlugin.m b/src/ios/FacebookConnectPlugin.m index fb728c341..394738939 100644 --- a/src/ios/FacebookConnectPlugin.m +++ b/src/ios/FacebookConnectPlugin.m @@ -23,6 +23,7 @@ - (NSDictionary *)responseObject; - (NSDictionary*)parseURLParams:(NSString *)query; - (BOOL)isPublishPermission:(NSString*)permission; - (BOOL)areAllPermissionsReadPermissions:(NSArray*)permissions; +- (void)enableHybridAppEvents; @end @implementation FacebookConnectPlugin @@ -52,6 +53,7 @@ - (void) applicationDidFinishLaunching:(NSNotification *) notification { - (void) applicationDidBecomeActive:(NSNotification *) notification { [FBSDKAppEvents activateApp]; + [self enableHybridAppEvents]; } #pragma mark - Cordova commands @@ -649,6 +651,27 @@ - (BOOL)areAllPermissionsReadPermissions:(NSArray*)permissions { return YES; } +/* + * Enable the hybrid app events for the webview. + * This feature only works with WKWebView so until + * Cordova iOS 5 is relased + * (https://cordova.apache.org/news/2018/08/01/future-cordova-ios-webview.html), + * an additional plugin (e.g cordova-plugin-wkwebview-engine) is needed. + */ +- (void)enableHybridAppEvents { + if ([self.webView isMemberOfClass:[WKWebView class]]){ + NSString *is_enabled = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"FacebookHybridAppEvents"]; + if([is_enabled isEqualToString:@"true"]){ + [FBSDKAppEvents augmentHybridWKWebView:(WKWebView*)self.webView]; + NSLog(@"Hybrid app events are enabled!"); + } else { + NSLog(@"Hybrid app events are not enabled!"); + } + } else { + NSLog(@"Hybrid app events cannot be enabled, this feature requires WKWebView"); + } +} + # pragma mark - FBSDKSharingDelegate - (void)sharer:(id)sharer didCompleteWithResults:(NSDictionary *)results {