Coder Social home page Coder Social logo

xamarin-forms-google-analytics's Introduction

Mobile App analytics using the Google analytics

This utility code helps in capturing mobile application analytics using the google analytics to measure and optimise user acquisition and engagement.

With easy-to-use SDKs for Android and iOS, helpful classes and methods to track user activities, one can generate useful statistics. It allows users to discover the popular screen or a flow through the application screens.

Google Analytics for Mobile Apps enables you to:

  • Understand the number of app users.
  • User behaviour and demographic.
  • Customize reports as per your need.
  • Visualize user navigation paths.
  • List of most popular devices with their OS versions, brands and screen resolution related information.
  • Visualise crash reporting and exception handling.

Available on

![icons] (https://thedistance.co.uk/wp-content/uploads/2015/10/ios.android.icon_.jpg)

Installation and Usage

Import packages for Android and iOS.

For Android

Brief: Using the following code user can track/analyse the following activities.

  • Tracking page name
  • Tracking event
  • Tracking exception
using System;
using Android.Content;
using Android.Gms.Analytics;
using Xamarin.Forms;
[assembly: Dependency(typeof(GoogleAnalyticsTest.Droid.GAService))]
namespace GoogleAnalyticsTest.Droid
{
    public class GAService : IGAService
    {
        //replace with your tracking id https://analytics.google.com/
        public string TrackingId = "UA-88590627-1";

        private static GoogleAnalytics GAInstance;
        private static Tracker GATracker;

        #region Instantiation ...
        private static GAService thisRef;
        public GAService()
        {
            // no code req'd
        }

        public static GAService GetGASInstance()
        {
            if (thisRef == null)
                // it's ok, we can call this constructor
                thisRef = new GAService();
            return thisRef;
        }
        #endregion

        public void Initialize_NativeGAS(Context AppContext = null)
        {
            GAInstance = GoogleAnalytics.GetInstance(AppContext.ApplicationContext);
            GAInstance.SetLocalDispatchPeriod(10);
            GATracker = GAInstance.NewTracker(TrackingId);
            GATracker.EnableExceptionReporting(true);
            GATracker.EnableAdvertisingIdCollection(true);
            GATracker.EnableAutoActivityTracking(true);
        }

        public void Track_App_Page(String PageNameToTrack)
        {
            GATracker.SetScreenName(PageNameToTrack);
            GATracker.Send(new HitBuilders.ScreenViewBuilder().Build());
        }

        public void Track_App_Event(String GAEventCategory, String EventToTrack)
        {
            HitBuilders.EventBuilder builder = new HitBuilders.EventBuilder();
            builder.SetCategory(GAEventCategory);
            builder.SetAction(EventToTrack);
            builder.SetLabel("AppEvent");
            GATracker.Send(builder.Build());
        }

        public void Track_App_Exception(String ExceptionMessageToTrack, Boolean isFatalException)
        {
            HitBuilders.ExceptionBuilder builder = new HitBuilders.ExceptionBuilder();
            builder.SetDescription(ExceptionMessageToTrack);
            builder.SetFatal(isFatalException);
            GATracker.Send(builder.Build());
        }
    }

}

Using the above code, you can view the following results on Google Analytics

![Fig1] (http://52.77.35.210:83/Media/Default/images/githubimages/Google%20Analytic_Fig1.png)

Fig 1- Overview with active users, active screens and app versions

![Fig2] (http://52.77.35.210:83/Media/Default/images/githubimages/Google%20Analytic_Fig2.png)

Fig 2- Tracking events with category, action and active users.

![Fig3] (http://52.77.35.210:83/Media/Default/images/githubimages/Google%20Analytic_Fig3.png)

Fig 2- Tracking crashes and exceptions.

For iOS

Brief: Using the following code user can track/analyse the following activities.

  • Tracking screen (Page) name
  • Tracking event
  • Tracking exception
using System;
using Foundation;
using Google.Analytics;
using Xamarin.Forms;
[assembly: Dependency(typeof(GoogleAnalyticsTest.iOS.GAService))]
namespace GoogleAnalyticsTest.iOS
{
    public class GAService : IGAService
    {
        //replace with your tracking id https://analytics.google.com/
        public string TrackingId = "UA-88590627-1";
        public ITracker Tracker;
        const string AllowTrackingKey = "AllowTracking";

        #region Instantition...
        private static GAService thisRef;
        public GAService()
        {
            // no code req'd
        }

        public static GAService GetGASInstance()
        {
            if (thisRef == null)
                // it's ok, we can call this constructor
                thisRef = new GAService();
            return thisRef;
        }
        #endregion

        public void Initialize_NativeGAS()
        {
            var optionsDict = NSDictionary.FromObjectAndKey(new NSString("YES"), new NSString(AllowTrackingKey));
            NSUserDefaults.StandardUserDefaults.RegisterDefaults(optionsDict);

            Gai.SharedInstance.OptOut = !NSUserDefaults.StandardUserDefaults.BoolForKey(AllowTrackingKey);

            Gai.SharedInstance.DispatchInterval = 10;
            Gai.SharedInstance.TrackUncaughtExceptions = true;

            Tracker = Gai.SharedInstance.GetTracker("Test App", TrackingId);
        }

        public void Track_App_Page(String PageNameToTrack)
        {
            Gai.SharedInstance.DefaultTracker.Set(GaiConstants.ScreenName, PageNameToTrack);
            Gai.SharedInstance.DefaultTracker.Send(DictionaryBuilder.CreateScreenView().Build());
        }

        public void Track_App_Event(String GAEventCategory, String EventToTrack)
        {
            Gai.SharedInstance.DefaultTracker.Send(DictionaryBuilder.CreateEvent(GAEventCategory, EventToTrack, "AppEvent", null).Build());
            Gai.SharedInstance.Dispatch(); // Manually dispatch the event immediately
        }

        public void Track_App_Exception(String ExceptionMessageToTrack, Boolean isFatalException)
        {
            Gai.SharedInstance.DefaultTracker.Send(DictionaryBuilder.CreateException(ExceptionMessageToTrack, isFatalException).Build());
        }
    }
}

To get this code readymade take the following steps:

  • Step 1 - Create account on Google analytics
  • Step 2 - Copy google analytics tracking ID (e.g.: UA-xxxxxxxx-1)

![Fig4] (http://52.77.35.210:83/Media/Default/images/githubimages/Google%20Analytic_Fig4.png)

  • Step 3 - Go to GitHub, Down load code using URL: https://github.com/iWorkTech
  • Step 4- Unzip the code and open the solution file in Visual Studio.
  • Step 5 โ€“ Replace Tracking ID with yours google analytics tracking ID.
  • Step 6 โ€“ Run the code.

xamarin-forms-google-analytics's People

Contributors

vaishalilimaye avatar ashedge avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.