After my previous post about how to add Google Analytics to your Xamarin Android app, I thought of writing an article for Xamarin iOS as well based on my own experience.🙂
When I was implementing Google Analytics in one of my Xamarin iOS projects, it wasn’t such a complicated process, thanks to the official Xamarin component for GA (Google Analytics) but then again, I couldn’t find much articles based on its code implementation, whereas I had to begin with simple example they have given in the Xamarin Component Store.
Now the whole concept of the Google Analytics implementation in Xamarin iOS is pretty much same as the implementation in Xamarin Android, but the coding approach and syntax are different.
Therefore I thought of sharing my experience and coding approach with you fellow Xamarin developers out there, which I’m pretty sure will be very much helpful for you all.😉
Setting up the Library…
First of all, follow me and praise the official Google Analytics component for Xamarin iOS…😉 Go ahead download it..
Once you have downloaded, we need to add the DLLs to your project References. You can do that by navigating into the following path in the folder you downloaded and extracted the library files to, which should be something similar to this “C:\…\googleanalytics-3.0.12\googleanalytics-3.0.12\lib\ios-unified\”. Add the “GoogleAnalytics.iOS.dll” to your project references.
Please note that this is for the Xamarin iOS Unified SDK, not for the classic SDK !🙂 I wouldn’t be going into the class sdk as everyone is migrating to the ios unified SDK.
Let the Coding begin…
Just as I have done in my previous article, I recommend that you keep a separate class for handling all the Google Analytics functionalities. So go ahead and create GAService class. In this class we are gonna implement all the GA functionalities we need to execute.
As for initializing we need to have instances with in this class as below and make sure to add the tracking code you get from your Google Analytics dashboard to the TrackingId value…
As you can see above, except for the Google Analytics instance I have added a string called “AllowTrackingKey“, this is going to be used for allowing the user to opt out of the analytics tracking upon their choice. I have just added it to this code, for the sake of good practices, but you can remove it from here and later in the code implementation if you wish to.
In the constructor, we need to initialize the Google Analytics tracking service, and for that we need to have a reference and we do not need to pass in any references like we did in Xamarin Android.🙂 Because in Xamarin GA it keeps a static shared instance of the GA tracking service where we can easy access.
As you can see above I have used the previously explained AllowTrackingKey variable to give the user option to opt out from the Tracking events by adding it to the SharedUserDefaults.
Here we are using the Shared instance that Xamarin GA provides in order to instantiate it with our configuration. There up above as you can see I have set the Local dispatch period to “10”, which means all the tracking data will be automatically uploaded to the online GA service in every 10 seconds, this is a good approach considering we should not exhaust the network resources by uploading tracking information every single second. You can change the value to anything you wish but I recommend you keep it around this value.
For some reason exception for the AutoExceptionTracking I couldn’t find any methods or properties to enable AutoActivityTracking and AdvertisingIdCollection, like we did in Xamarin Android. Probably they can not be supported in iOS platform.
Now that we have done with the initialization, next let’s dive into coding the actions… 😉
Let’s track a Page View…
In order to track a Page you may need to add the following method to our GAService class.
As you can see above we are passing in the Page Name we need to track and we are setting it to our GA tracker instance . Then within the Send() method call we are creating a new Screen View with the GAIDictionaryBuilder (there are several types of GAI Dictionary Builders which you could use based on several types events you need to track), specifically calling the CreateScreenView(), which is used to create a new a screen view hit. Keep in mind whatever the name you pass in to the method will be recorded in Google Analytics, whenever we need to track a page view, we will be calling this method.
Let’s track an Event…
In order to track any Event, such as a button click, check box selection and son on, you can use the following method. This is very much likely how we did in Xamarin Android, but the syntaxes are a bit different.
In order to track an Event in Google Analytics, we need to have an Event Category, therefore we need to create a small struct along with the type of categories you need to track, this actually depends completely upon your requirement. You can have types of event categories such as. “Sales”, “Order”, “Shopping Cart” and so on… 🙂
Now let’s implement the method, for this method we will be passing the above struct values for the GAEventCategory parameter. And for the EventToTrack parameter, you can send whatever event/ event description you want to record in Google Analytics.
As shown above we are using the GAIDictionaryBuilder with the CreateEvent method call to create a new event and we are setting up the category and event action as it’s passed in as parameters. Also you can set Labels in Google Analytics for any of your events, but as a general I have set the default label to “AppEvent”, but you can use labeling if you want to generate more complex and detailed tracking, where as Google Analytics is a truly great tool.
Also you may notice that I have called the method Dispatch() right at the bottom, this is just to show you that, we can manually dispatch the Analytics data on demand as we wish, without waiting for the default dispatch to occur.🙂 Like wise even in other events you can call the dispatch method and immediate send the tracking data to GA servers, but I wouldn’t recommend that as it could clog up your bandwidth usage.
Let’s track an Exception…
Now we all know that exceptions O.o are such a nasty bastard in any application. And its a good habit to keep a track on them. As I have mentioned above,
This Tacker setting captures all the Exceptions that occurs in the application, but keep in mind, this only tracks the unhandled exceptions. Now how about the handled exceptions ? what if we need to keep a track on a certain type of exceptions that’s already being handled in the application.
For such instances you may use the following piece of code… 😉 Just like we did in Xamarin Android, if you want to record an Exception event in Google Analytics, you need to mention whether it’s a Fatal Exception or not. Therefore you need to pass in a Boolean mentioning whether it’s fatal or not as coded in the below method.
We are using the GAIDictionaryBuilder with CreateException method call for this hit in the same method I have used in the other two events above. And when you pass in the Exception message, you can either take the whole ex.StackTrace() or ex.Massage() or both together as a value of your exception in anyway you wish. :)
And now just like we did in Xamarin Android we need to do some decorations to our GAService class…
Just a little decorating…
Now I have unified the access to the Google Analytics tracking by including all the methods in one single class, but in order to access it we need to instantiate right ? That’s why we need to add a little Singleton pattern decoration to our class. Or else you could also make the methods static and straightaway call them, but its your wish.. 🙂
Now that way we can keep a single instance and use it to execute all the functions.
After all that’s done your GAService class should look like this… 🙂
Finally let’s FIRE IT UP… ! 😉
Well as our GAService class is ready, let’s see how to access it and start tracking our App. 🙂
You need to modify your FinishedLaunching method as follows,
As you can see above I am first of all initializing our Google Analytics services through the GetGASInstance() method.
Here is how you could use our above class for Page Tracking, Event Tracking and Exception Tracking.
After calling those methods you will be able to see the live tracking information in your Google Anlytics Dashboard online. Just log in to the Real-Time Overview and you will be able to see everything there. Just keep in mind, as there is a dispatch period of 10 seconds, it might take a while for the actual tracking data to be displayed in the Real-Time tracking view in Google Analytics… 🙂
Well there it goes folks, hope this post was helpful for you and prolly saved you a lot of time…🙂 Please share this among other developers as there aren’t much tutorials on Google Analytics tracking for Xamarin iOS, and you may save another developer’s time as well…😉
Cheers ! Stay Awesome ^_^ !