Skip to content
This repository has been archived by the owner on Oct 24, 2022. It is now read-only.

Commit

Permalink
Enable app events to be sent from the pixel for both iOS and Android (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
msencer committed Jan 17, 2019
1 parent cf4728a commit 1ae8521
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
9 changes: 8 additions & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
</description>

<repo>https://github.com/jeduan/cordova-plugin-facebook4</repo>

<license>Apache 2.0</license>

<preference name="APP_ID" />
<preference name="APP_NAME" />
<preference name="FACEBOOK_HYBRID_APP_EVENTS" default="false" />
<preference name="FACEBOOK_ANDROID_SDK_VERSION" default="4.38.1"/>

<engines>
Expand Down Expand Up @@ -52,11 +53,13 @@
<config-file target="res/values/facebookconnect.xml" parent="/*">
<string name="fb_app_id">$APP_ID</string>
<string name="fb_app_name">$APP_NAME</string>
<bool name="fb_hybrid_app_events">$FACEBOOK_HYBRID_APP_EVENTS</bool>
</config-file>
<!-- Used for cordova-android 7 -->
<config-file target="app/src/main/res/values/facebookconnect.xml" parent="/*">
<string name="fb_app_id">$APP_ID</string>
<string name="fb_app_name">$APP_NAME</string>
<bool name="fb_hybrid_app_events">$FACEBOOK_HYBRID_APP_EVENTS</bool>
</config-file>

<config-file target="AndroidManifest.xml" parent="application">
Expand Down Expand Up @@ -104,6 +107,10 @@
<string>$APP_NAME</string>
</config-file>

<config-file target="*-Info.plist" parent="FacebookHybridAppEvents">
<string>$FACEBOOK_HYBRID_APP_EVENTS</string>
</config-file>

<config-file target="*-Info.plist" parent="CFBundleURLTypes">
<array>
<dict>
Expand Down
23 changes: 23 additions & 0 deletions src/android/ConnectPlugin.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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<String, String> paramBundle) {
ShareLinkContent.Builder builder = new ShareLinkContent.Builder();
if (paramBundle.containsKey("href"))
Expand Down
23 changes: 23 additions & 0 deletions src/ios/FacebookConnectPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ - (NSDictionary *)responseObject;
- (NSDictionary*)parseURLParams:(NSString *)query;
- (BOOL)isPublishPermission:(NSString*)permission;
- (BOOL)areAllPermissionsReadPermissions:(NSArray*)permissions;
- (void)enableHybridAppEvents;
@end

@implementation FacebookConnectPlugin
Expand Down Expand Up @@ -52,6 +53,7 @@ - (void) applicationDidFinishLaunching:(NSNotification *) notification {

- (void) applicationDidBecomeActive:(NSNotification *) notification {
[FBSDKAppEvents activateApp];
[self enableHybridAppEvents];
}

#pragma mark - Cordova commands
Expand Down Expand Up @@ -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<FBSDKSharing>)sharer didCompleteWithResults:(NSDictionary *)results {
Expand Down

0 comments on commit 1ae8521

Please sign in to comment.