Converting facebook pixel event to app event in hybrid mobile app

1.2k Views Asked by At

I am trying to send App Events from Hybrid mobile app(cordova). Through Facebook Pixel event can be sent but as stated by this blog - "Sending App Events in the context of a mobile application can provide a number of advantages over sending pixel events".

Events for hybrid mobile apps as per this facebook documentation mentions it is possible to convert Facebook Pixel event into App Events. The total code change required in your application is two lines (one native, one web).

For web fbq('init', ); fbq('set', 'mobileBridge', , );

For Native: android: AppEventsLogger.augmentWebView(, )

ios: [FBSDKAppEvents augmentHybridWKWebView:];

So my question is where to updated this native part code in case of android and ios in cordova app?

2

There are 2 best solutions below

3
David_B On

Documentation: https://developers.facebook.com/docs/app-events/hybrid-app-events/

It states (the full statement): "When you augment a webview from iOS or Android, we inject a window level object called fbmq_. The Pixel, when set up to look for a mobile bridge, finds this window level fbmq object each time an fbq call is made."

Let`s brake it down:"When you augment a webview from iOS or Android..."

Android:

AppEventsLogger.augmentWebView(<YOUR_WEBVIEW_OBJECT>, <YOUR_ANDROID_CONTEXT>)

iOS:

[FBSDKAppEvents augmentHybridWKWebView:<YOUR_WK_WEB_VIEW_OBJECT>];

"...we inject a window level object called fbmq_. The Pixel, when set up to look for a mobile bridge, finds this window level fbmq object each time an fbq call is made.""

Setup should look something like this:

 fbq('init', '');
    if (window.cordova){
      fbq('set', 'mobileBridge', '', '');
}

Disclaimer: This is not tested and is an only theoretical implementation based on the documentation that Facebook provides.

0
Akram Khan On

Hope it helps!

Download FB connect Cordova Plugin from here : https://github.com/cordova-plugin-facebook-connect/cordova-plugin-facebook-connect (This plugin will help you to track your hybrid app events)

cordova plugin add cordova-plugin-facebook-connect --save --variable APP_ID="123456789" --variable APP_NAME="myApplication" --variable FACEBOOK_HYBRID_APP_EVENTS="true"

The above plugin auto enables "AppEventsLogger.augmentWebView(<YOUR_WEBVIEW_OBJECT>, <YOUR_ANDROID_CONTEXT>)"

However if for some reason it does not work then

Navivate to:: platforms\android\app\src\main\java\org\apache\cordova\facebook\ConnectPlugin.java

code snapshot