Using Analytics Service

Version 1

    Analytics build into concierge-sdk can track and report different analytics events like page views , clicks and more to a custom Analytics Service running on the device.

    Please check AnalyticsManager page on how it can be instantiated within your application and what else can be tracked with it. Here is a code snippet of how you can instantiate AnalyticsManager and track a button click within an application.


    @Override

      public void onProductClick(final Cursor data)

      {

           if (!mProductClicked)

           {

                // Set a flag to prevent multiple activity launches

                mProductClicked = true;    

     

                // Start the product detail activity when a product in the grid is clicked

                long productId = data.getLong(data.getColumnIndex(Catalog.ProductColumns._ID));

                Intent intent = new Intent(Intent.ACTION_VIEW);

                intent.setData(Catalog.Products.getProduct(productId));


               AnalyticsManager analytics = (AnalyticsManager) getSystemService(ContextExtension.ANALYTICS_SERVICE);

               analytics.trackClick("Product", intent.getDataString());

               startActivity(intent);

           }

      }

     

    Now you need to have a AnalyticsService running on your device that will get those events from the AnalyticsManager for processing them . Please check AnalyticsService documentation page for more information on how to incorporate it on your Analytics Application. Here are some code snippets from an application that extends AnalyticsService to handle the events sent from the AnalyticsManager. Furthermore the app uses GoogleAnalytics for web APIs to report those events to the GoogleAnalytics Server. Error handling and null checks are not shown on the code. This is just a sample app , you can write your own analytics app and report it to analytics server of your choice , google analytics is one of the many ways to do so.

     

    On the app manifest file

     

    <!-- Google analytics service -->

            <service

                android:name=".GoogleAnalyticsService"

                android:permission="concierge.permission.START_ANALYTICS_SERVICE">

                <intent-filter>

                    <action android:name="concierge.analytics.AnalyticsService" />

                </intent-filter>

            </service>

     

    Creating the service

     

    import com.google.android.apps.analytics.GoogleAnalyticsTracker;

    import concierge.analytics.model.AnalyticsDevice;

    import concierge.analytics.model.AnalyticsEvent;

    import concierge.analytics.model.AnalyticsPageview;

    import concierge.analytics.model.AnalyticsSession;

    import concierge.analytics.AnalyticsService;

     

    import concierge.content.ContextExtension;

     

    public class GoogleAnalyticsService extends AnalyticsService

    {

            private GoogleAnalyticsTracker mTracker;     

     

    private static final int DEVICE_LOCATION_INDEX = 1;

    private static final String DEVICE_LOCATION_NAME = "location";

     

    private static final int APPLICATION_PACKAGE_INDEX = 4;

    private static final String APPLICATION_PACKAGE_NAME = "app";

     

     

         @Override

           public void onCreate()

           {

                super.onCreate();

                mTracker = GoogleAnalyticsTracker.getInstance();

                mTracker.setUseServerTime(false);

                mTracker.setSampleRate(SAMPLE_RATE);

           }

     

    @Override

      public void onSessionStart(AnalyticsSession session)

      {

      AnalyticsDevice device = session.getDevice();

      mTracker.setCustomVar(DEVICE_LOCATION_INDEX, DEVICE_LOCATION_NAME, device.getLocation(), SCOPE_VISITOR);

     

      // Start the new session

      mTracker.startNewSession(ManagedAccountID, DISPATCH_PERIOD_SEC, this);

      }

     

      @Override

      public void onEvent(AnalyticsEvent event)

      {

      // Set page scope custom variables

      mTracker.setCustomVar(APPLICATION_PACKAGE_INDEX, APPLICATION_PACKAGE_NAME, event.getPackageName(), SCOPE_PAGE);

     

      // Track the event

      mTracker.trackEvent(event.getCategory(), event.getAction(), event.getLabel(), event.getValue());

      }

     

      @Override

      public void onPageview(AnalyticsPageview pageview)

      {

      // Set page scope custom variables

      mTracker.setCustomVar(APPLICATION_PACKAGE_INDEX, APPLICATION_PACKAGE_NAME, pageview.getPackageName(), SCOPE_PAGE);

     

      // Track the pageview

      mTracker.trackPageView(pageview.getUri());

      }

     

      @Override

      public void onSessionEnd(long endTime)

      {

           mTracker.stopSession();

      }

     

      @Override

      public void onDestroy()

      {

           super.onDestroy();

           mTracker = null;

      }

    }