How do I add Google Analytics to a large Windows Phone 8.1 project

227 views Asked by At

I have a Windows Phone 8.1 Project that does something similar to youtube. (The windows phone version). Now I want to integrate Google analytics to it, I saw an article on visual studio magazine on how to do it for page views, events, and exception. The challenge is, this project has a lot of pages and views, a lot of events, and handled exceptions (try catch block). Is there a fast way to put this Google Analytics sdk codes instead of writing in the code on each page, event or exception one after the other (I mean that would be tedious)

2

There are 2 answers

1
Igor Kulman On

First, choose the right 3rd party SDK, because there is no official one, of course. I would recommend https://googleanalyticssdk.codeplex.com/.

To make things easier, you can hook into the Frame`s navigated event to easily log page views for every page, an exaple (but using AppInsights)

    /// <summary>
    /// Handles application navigation
    /// Reports current View to analytics
    /// </summary>
    /// <param name="sender">Frame</param>
    /// <param name="e">NavigationEventArgs</param>
    private void FrameNavigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
    {            
        try
        {
            var viewName =
                e.Uri.ToString().Replace("//", "/").Replace("/Views/", "").Split('.').First().Replace("View", "");
            AnalyticsHelper.TrackPageView(viewName);
        }
        catch (Exception ex)
        {
            AnalyticsHelper.TrackException("FrameNavigatedTelemetry", ex, e.Uri != null ? e.Uri.ToString() : "");
        }
    }

Or if you have a common base ViewModel for all your ViewModels you can put reporting there. But events, exceptions, you will have to do it manually.