Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Enable Facebook Analytic in Android Application?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 1.79k
    Comment on it

    With around 1.7 billion users and which are growing day by day, Facebook has now come a very important medium to promote your app and website world wide.

     

     

    Facebook along with social sharing provides many other tools and options to track the reports of your business/product page engagement and the tool is Facebook Analytics. It helps the page owner to understand and analyse, how users are using your app or website. So, in this tutorial Enable Facebook Analytic in Android Application.

    "For more on FB analytics you can visit the Facebook official page: https://developers.facebook.com/docs/analytics/overview/

     

    To use this feature you have to make your app enabled with Facebook analytics, for that you need to go through following steps:

     

    1. Create a new app on facebook portal making use of “https://developers.facebook.com/products/analytics/quickstarts/android/” link.

     

    2. Now you need to import facebook sdk to your project

     

    • Create a new app on your android Studio with minimum API level 15:Android 4.0.3 that is IcreamSandwich or else you can choose greater version than the mentioned one.

     

    • Now in your project open Gradel Script then select the build.gradle(project) level and add the mavenCentral() in buildscript { repositories {}} section as shown below.

    buildscript {
        repositories {
            jcenter()
    
            mavenCentral()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:2.3.0'
    
            // NOTE: Do not place your application dependencies here; they belong
            // in the individual module build.gradle files
        }
    }
    
    allprojects {
        repositories {
            jcenter()
        }
    }
    
    task clean(type: Delete) {
        delete rootProject.buildDir
    }
    

     

    • After adding this click on the build.gradle file and add the line compile'com.facebook.android:facebook-android-sdk:[4,5)' in yor dependencies{} section as shown below.

    dependencies {
        compile fileTree(include: ['*.jar'], dir: 'libs')
        androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
            exclude group: 'com.android.support', module: 'support-annotations'
        })
        compile 'com.android.support:appcompat-v7:25.3.1'
        compile 'com.android.support.constraint:constraint-layout:1.0.2'
        compile 'com.facebook.android:facebook-android-sdk:[4,5)'
        testCompile 'junit:junit:4.12'
    }
    

     

    3. Now you need to add facebook app id

    open your string file in your project copy your app id from facebook portal and paste it in string file as shown below


    4.Now use Internet permission by adding <uses-permission android:name="android.permission.INTERNET"/> in your manifest file

    and do add the below given code to your manifest file

    
    
    <meta-data android:name="com.facebook.sdk.ApplicationId" 
            android:value="@string/facebook_app_id"/>
        
        <activity android:name="com.facebook.FacebookActivity"
            android:configChanges=
                    "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:label="@string/app_name" />
        <activity
            android:name="com.facebook.CustomTabActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="@string/fb_login_protocol_scheme" />
            </intent-filter>
        </activity>

    5. Now add your app package name to facebook portal @ https://developers.facebook.com/products/analytics/quickstarts/android/

     

    6. Now add devlopment or release key hash to facebook portal by running the following command in your terminal for linux

    keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64

    This will give you an output somthing like this ga0RGNYHvNM5d0SLGQfpQWAPGJ8= known as key hash add this to your app on facebook portal.

    Now make simple facebook login and see the login events on facebook analytics portal.

    Below I have shown the code for login event by making use of facebook login button .

    public class LandingScreen extends AppCompatActivity {
    
    
        private LoginButton loginButton;
        private CallbackManager callbackManager;
        private LayoutInflater inflater;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_landing_screen);
    
            FacebookSdk.sdkInitialize(getApplicationContext());
            AppEventsLogger.activateApp(this);
    
    
    
            callbackManager = CallbackManager.Factory.create();
    
            loginButton = (LoginButton)findViewById(R.id.login_button);
            loginButton.setReadPermissions("email");
            // If using in a fragment
    
            // Other app specific specialization
    
            // Callback registration
            loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    // App code
                }
    
                @Override
                public void onCancel() {
                    // App code
                }
    
                @Override
                public void onError(FacebookException exception) {
                    // App code
                }
            });
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            callbackManager.onActivityResult(requestCode, resultCode, data);
        }
    }
    

     

    Below is the XML for the above Activity

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="android.manjheeyup.com.manjheeapp.LandingScreen">
    
        <com.facebook.login.widget.LoginButton
            android:id="@+id/login_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="30dp"
            android:layout_marginBottom="30dp" />
    
    </android.support.constraint.ConstraintLayout>

     

    How to Enable Facebook Analytic in Android Application?

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: