Facebook Analytics for Apps with Xamarin Forms

Facebook Analytics for Apps helps you better understand how people are using your iOS and Android apps. The Analytics for Apps shows you data, trends and charts with built-in graphs showing the actions that people take in your app, from counting app Launches, details of viewed content, searching or purchasing.  

 Facebook Analytics Active Users

Analytics for Apps provides aggregated, audience insights to identify demographics, interests and other attributes of the people using your app.

The implementation is pretty straight forward.

Setting up Facebook app analytics

Firstly you need to set up your facebook app page, instructions are available here.

 

Configure the Facebook app for iOS here , this process will generate some xml that you need to copy and paste into your info.plist file and add your bundle identifier to the Facebook app settings.

 

To configure the Facebook app for Android here, this process will generate xml that you need to copy and paste into your strings.xml and AndroidManifest.xml files and add your bundle identifier to the Facebook app settings.

 

Setting up your Xamarin Forms app for Facebook analytics

You will need to add the SDK bindings from either Nuget of the Xamarin Component Store.

Facebook SDK for iOS on Xamarin Component store or Facebook SDK for iOS on Nuget

Facebook SDK for android by Xamarin or Facebook SDK for Android on Nuget

Once the packages are installed and you have added the AndroidManifest.xml and info.plist entries you need to call the SDK App Events to notify Facebook that the app has been opened.

The best place to make these calls in Xamarin forms is in the App Class, OnStart() and OnResume() methods. In the example below, the compiler rules have been used to call the Android and iOS SDK separately as the syntax is different in each SDK.

 

   protected override void OnStart()
        {
            // Handle when your app starts
            //ReStartSchedule();
            #if __ANDROID__
                Xamarin.Facebook.AppEvents.AppEventsLogger.ActivateApp(Xamarin.Forms.Forms.Context);
            #endif

            #if __IOS__
                Facebook.CoreKit.AppEvents.ActivateApp();
            #endif
        }

        protected override void OnSleep()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume()
        {
            // Handle when your app resumes
            #if __ANDROID__
                Xamarin.Facebook.AppEvents.AppEventsLogger.ActivateApp(Xamarin.Forms.Forms.Context);
            #endif

            #if __IOS__
                Facebook.CoreKit.AppEvents.ActivateApp();
            #endif
        }
  

 That's it, fire up your app and you should see your events in the App event debugging screen 

https://www.facebook.com/analytics/<<your app id>>/?section=most_recent.

Facebook analytics and Xamarin forms

Posted on 25/05/2016 10:26:45Xamarin