Friday 27 February 2015

HB Blog 60: Google Analytics Android Integration.

Gooe Analytics is a service offered by Google that generates detailed statistics about a website's and mobile traffic and their traffic sources and measures conversions and sales.The Google Analytics SDK for Android makes it easy for developers to collect user engagement data from their apps. Additionally, the Google Analytics SDK for Android gives you the tools to monitor the success of mobile marketing campaign by providing end-to-end visibility into the performance of your marketing channels, from Google Play installs through in-app purchases and transactions.

Developers can then use Google Analytics reports to measure:
    The number of active users are using their applications.
    From where in the world the application is being used.
    Adoption and usage of specific features.
    In-app purchases and transactions.
    The number and type of application crashes.
    And many other useful metrics.

There are following steps to getting started with the SDK,
1)First login to Google Analytics site and get tracking id for your project.
2)Then, add Google Analytics library(libGoogleAnalyticsServices.jar) to your project and clear the dependencies that are required.
3)Then add following files and code snippnet to make it run.
    Update AndroidManifest.xml - For network permissions.
    Add EasyTracker methods - For initiating Google Analytics.
    Create your analytics.xml file - For mainting unique tracking details via tracking code.

Refer the below link for complete sample code:-
Download Sample Code
Download Apk File
Have a look on few code snippets,

Here I have create array bound exception as well as Demo Event which will be tracked by Google Analytics,
//MainActivity.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button=(Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int a[] = new int[2];
                System.out.println("Access element three :" + a[3]);
            }
        });

        // May return null if a EasyTracker has not yet been initialized with a
        // property ID.
        EasyTracker easyTracker = EasyTracker.getInstance(this);
        // MapBuilder.createEvent().build() returns a Map of event fields and values
        // that are set and sent with the hit.
        easyTracker.send(MapBuilder
                        .createEvent("Demo Event category",     // Event category (required)
                                "Demo Event action",  // Event action (required)
                                "Demo Event label",   // Event label
                                (long)1234)            // Event value
                        .build()
        );
    }

    @Override
    public void onStart() {
        super.onStart();
        // Google Analytics Tracker onStart() code.
        EasyTracker.getInstance(this).activityStart(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        // Google Analytics Tracker onStop() code.
        EasyTracker.getInstance(this).activityStop(this);
    }
}

//analytics.xml
1
2
3
4
5
6
7
8
<resources><!-- xmlns:tools="http://schemas.android.com/tools" tools:ignore="TypographyDashes" -->
<!-- Replace placeholder ID with your tracking ID -->
<string name="ga_trackingId">UA-xxxxxxxxx-X</string>
<!-- Enable automatic activity tracking -->
<bool name="ga_autoActivityTracking">true</bool>
<!-- Enable automatic exception tracking -->
<bool name="ga_reportUncaughtExceptions">true</bool>
</resources>

No comments:

Post a Comment