Rabu, 15 Desember 2010

Analytics for Android Apps

Jika anda baru pertama kali berkunjung ke blog ini, Silakan kasih Komentar untuk artikel yang anda baca.

[This post is by Alexander Lucas, an Android Developer Advocate bent on saving the concern 5 minutes.Tim Bray]

With the constituent of bespoken variables to the Mobile Analytics SDK for Android, it strikes me as a beatific instance to cover something some of you strength not hit known was possibleusing Google Analytics to easily road app usage. Using the ambulatory SDK is a handy artefact to intend actualised accumulation on how users interact with your Android apps. So today I'm feat to vindicate how to road practice of your covering with Google Analytics.

Prereqs Ahoy!

Before you verify soured running with this shiny newborn toy, theres a few things youll need to ordered up first:

  • Download the ambulatory SDK. Download and artefact manual are acquirable in the getting started country of the Mobile SDK docs, but the summarized edition is:

    • Download the fix enter from the download page

    • Put the libGoogleAnalytics.jar enter in your projects /libs directory

    • Be sure the mass lines are in your AndroidManifest.XML file:<uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

  • Youre feat to need a Google Analytics account. Go to google.com/analytics and ordered up an statement if you dont already hit one. Then ordered up a strikingness for your Android application. When youre done youll wager a javascript snippet to append into your site. Copy the conception that looks same UA-XXXXXXX-X. Youll ingest this in the Android covering to tell Analytics which strikingness the accumulation is existence dispatched for.

Get Tracking

Previous Google Analytics users are feat to encounter a lot of this familiar. In fact, weve prefabricated a point of keeping the programme as familiar as possible.

First, intend your tracker object, and initialize it using the UA cipher for the Analytics strikingness you poverty to track. It makes the most significance to do this in the onCreate() method for your state main, so it exclusive fires when your covering starts up.

GoogleAnalyticsTracker tracker;protected vacuum onCreate(Bundle savedInstanceState) { ... tracker = GoogleAnalyticsTracker.getInstance(); tracker.start(UA-1234-1, this); }

The ambulatory SDK provides support for the 3 main types of accumulation dispatched to the Google Analytics servers: Pageviews, events, and bespoken variables.

Pageviews

A pageview is a accepted effectuation to manoeuvre traffic volume to a tralatitious website. Given that this is feat into an Android app and not a website, its feat to be up to you to decide what a pageview means. Depending on the type of app, apiece Activity or assorted views within the aforementioned state (for instance, assorted tabs within a TabActivity) could calculate as a pageview.

Whenever you poverty to trigger a pageview, call the trackPageView() method. It exclusive takes digit parameter, the URL you poverty a pageview counted towards.

tracker.trackPageView("/HomeScreen");

Pageviews attain the most significance as flooded screen transitions, which in most cases module stingy digit pageview per Activity. Therefor it makes the most significance to place the call to trackPageView in the onCreate() method for apiece state in your application. An exception would be if you were using a TabActivity, or other scenario where there were multiple full-screen transitions which every occurred within the aforementioned Activity, and conceptually mapped to seperate full-screen pages existence viewed.

Events

In Analytics, events are fashioned to road individual interaction to that doesnt map to pageviews, same touch play/pause/stop in a multimedia app. This maps rattling well to Android usageAny form of interaction, from touch certain buttons to adding/removing accumulation from the datastore, crapper be tracked using Events.

Events are a little more complicated than pageviews, but meet slightly. Instead of 1 parameter, you hit 4: Category, Action, Label (optional), Value (optional).

To wager how to attain ingest of these, lets imagine you had a media player application, and desired to road how some times play, pause, and stop were clicked. The cipher would countenance same this:

playButton.setOnClickListener(new OnClickListener() { @Override open vacuum onClick(View v) { ... tracker.trackEvent( "Media Player", // Category "Click", // Action "Play", // Label 0); // Value } }); pauseButton.setOnClickListener(new OnClickListener() { @Override open vacuum onClick(View v) { ... tracker.trackEvent( "Media Player", // Category "Click", // Action "Pause", // Label 0); // Value }); stopEventButton.setOnClickListener(new OnClickListener() { @Override open vacuum onClick(View v) { ... tracker.trackEvent( "Media Player", // Category "Click", // Action "Stop", // Label currentVideo.getPositionInSeconds()); // Value }); myMediaPlayer.setFinishedListener(new FinishedListener() { @Override open vacuum onFinished(View v) { ... tracker.trackEvent( "Media Player", // Category "Video Finished", // Action "Stop", // Label currentVideo.getLengthInSeconds()); // Value });

Remember that in the Google Analytics scheme interface, this accumulation is displayed hierarchicallyFor instance, if you click on Categories in the left nav, and then on Media Player, youll wager a list of every the assorted doable values of Action which hit happened in the media Player category. Clicking on Click module exhibit every the labels which were dispatched in the Media Player collection with an state of Click.

The 4th parameter, value, is optional, and behaves differently from the others. Its meant to be cumulative; In this example, Im sending the turn of recording watched when a recording is either stopped or allowed to finish. This is mass server-side, and when I go to countenance at my accumulation Ill be healthy to wager the total instance grouping hit spent watching videos using my application.

Custom Variables

The newborn hotness! Custom variables are name-value pair tags that you crapper append in your chase cipher in visit to refine Google Analytics tracking. The easiest artefact to think of this is as meta-data concomitant your pageviews and events. Using this metadata, it becomes cushy to split soured and countenance at segments of your data, much the aforementioned artefact you ingest labels in Gmail. One Android-specific example would be to hit a AppType status with Full or Lite depending on whether the individual has the flooded edition of the app or not. You could then ingest the Analytics scheme programme to countenance at exclusive the Lite users, and wager how their practice / userbase differs from the Full segment. Custom variables are a ridiculously powerful analytical tool, but theyre also a unfathomable topic. I cordially recommend gift the docs a once-through before implementing them in your Android application. Especially attain sure to feature the country on scoping. Twice. Im stingy it... Ill wait.

There are 4 parameters in a bespoken variable: Index (1 to 5 inclusive), Name, Value, and Scope (Optional, defaults to Page Scope).

The place in your cipher where setCustomVar() module be called depends mostly on what scope that uncertain module be:

  • Visitor scope: disposition erst the prototypal instance your covering is run on a device. Dont create any bespoken variables at the aforementioned index, or they module overwrite the prototypal one. Useful for sending accumulation about which edition of the app is existence used, what kind of phone, fatless vs flooded edition of the app, or anything that wont modify during the lifetime of the artefact of that application.

  • Session scope: disposition erst at the beginning of every Activity startup. Will administer to every pageviews and events for the lifecycle of the activity, unless a assorted bespoken uncertain is created at the aforementioned index.

  • Page scope: disposition correct before trackEvent or trackPageView that the bespoken uncertain should administer to, every instance that method is called. If no scope is specified, this is the default.

The call to ordered a bespoken uncertain module countenance same the following:

// schoolteacher are encoded to integers: Visitor=1, Session=2, Page=3tracker.setCustomVar(1, "Navigation type", "Button click", 3);

Choose a Dispatch Mode

In visit to behave for battery life, a request isnt actually dispatched out to the computer every instance you fire a pageview or bespoken variable. Instead, every the pageviews, events, and their related bespoken variables are stored in a topical SQLITE database until theyre dispatched as a group to the server. You crapper ordered this up to hap digit of two ways: Either hit the story become automatically every n seconds, or manually when you call story in code. The mode is chosen when you call the move method on your tracker.

Manual story looks same this:

// No instance process dispatched as a parametertracker.start(UA-1234-1, this);// disposition this when you poverty to beam the whole circumstance queue to the servertracker.dispatch();

The timed automatic story looks similar, but sends an extra constant (the sort of seconds between dispatches). In timed dispatch, you never hit to manually call dispatch.

// Dispatch every queued pagevies/events every 300 seconds (5 minutes)tracker.start("UA-YOUR-ACCOUNT-HERE", 300, this);

Its essential to advert that Google Analytics uses the timestamp for when it receives your data, not when the actualised pageview/event occurred. This crapper potentially advance to outside Analytics data, since events crapper be dispatched on assorted days than when they occurred, so verify care to story regularly.

The end result

Lets go backwards to that onCreate() method we utilised to instantiate the tracker earlier, and wager what it looks same with every the pieces in place:

GoogleAnalyticsTracker tracker;protected vacuum onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);tracker = GoogleAnalyticsTracker.getInstance();tracker.start(UA-1234-1, this);if(isFirstTimeRunningApplication()) { tracker.setCustomVar(1, App Type, Demo, 1);}tracker.trackPageView("/HomeScreen");}

How to countenance at every this data

There are two ways you crapper move this. First, Google Analytics has a pretty snazzy scheme interface, which does a rattling beatific job of surfacing useful aggregation for you. If youre newborn to Analytics and dont really know what youre looking for yet, the scheme programme is a enthusiastic artefact to explore your accumulation and understand your users.

If you already hit a brawny idea of the questions you poverty to ask (app practice crossways versions of the Android platform, ontogeny rates, time-in-app per demo individual vs flooded user, how some grouping beat level 3 on their prototypal try, etc), and meet poverty to automate the asking, Google Analytics also has a stylish accumulation export API, with computer libraries to facilitate the querying of your accumulation in Java, Python, JavaScript, and C#.

Abiding by the TOS

Google Analytics comes with its own TOS, and its essential to feature and abide by it. The essential bit, especially since this module be utilised exclusive Android applications, is that you cannot beam personally identifying information to Analytics servers. This is a big deal. It means, for instance, that a visitor-level bespoken uncertain cannot contain a sound number, prototypal name, or email address. Less intuitively, but ease important, it effectuation that if this covering is a computer to a scheme covering (say, CRM software or a shopping site), you also cannot store aggregation in Analytics which crapper be combined with your own backend software to refer the user, such as individual ID or a transaction ID same to the digit stored on your scheme backend.


Thanks
basyar.com

Tidak ada komentar:

Posting Komentar