Capture usage information of a Windows Phone 7 application using Google Analytics

About half a year ago I created a Blend behavior to capture usage information of a Silverlight application. But what’s next, in the near future we will have Windows Phone 7, and we can write very nice applications by using the Silverlight platform.

Although the platform is the same, you won’t be able to make use of the Blend behavior I’ve written before. Because this Blend behavior makes use of scripting inside the hosting html page. There simply is no page that hosts your Silverlight application. This is a similar problem we face when a Silverlight application is running out-of-browser, but back to the phone.

Solution Direction

To connect to Google Analytics I need to have a html page that contains a script that calls into Google Analytics to track events. So somehow I need to connect a Windows Phone 7 app to this html page.

Solution Implementation

I started off with the html page, and made it as small as possible, no content, only the scripting part.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Windows Phone 7 - Application Tracking</title>
    <script type="text/javascript">
        var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
        document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
	</script>
    <script type="text/javascript">
        var pageTracker = _gat._getTracker("UA-xxxxx-x");
        pageTracker._trackPageview();

        function trackEvent(category, action, label) {
            pageTracker._trackEvent(category, action, label);
        }
    </script>
</head>
<body>
</body>
</html>

I hosted it somewhere so it’s accessible through the internet. If you copy-paste this code please make sure that you fill in your own Google Analytics tracking code.

I’ve seen some code examples where Windows Phone 7 apps were hosting a webbrowser and where the app was communicating to the page hosted in the app. So that’s where I began as well.

Add a WebBrowser control to the MainPage.xaml, I named it trackingBrowser

<browser:WebBrowser x:Name="trackingBrowser"/>

Make sure that scripts are enabled on the WebBrowser control

image

In the end it’s not required to make it visible at all so we can set the Visibility to Collapsed. If you’re not comfortable with not showing the browser during development you can of course leave the browser visible.

image

The resulting xaml will look similar to this.

<browser:WebBrowser x:Name="trackingBrowser" 
                    IsScriptEnabled="True" 
                    Visibility="Collapsed" />

The WebBrowser needs to load the create html page. So in the Loaded event of the containing page I set the url of the WebBrowser.

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
    trackingBrowser.Navigate(new Uri("http://change-to-your-domain.com/tracking.htm"));
}

As soon as the WebBrowser navigated to this url I want to execute the trackEvent inside the html page. To call Javascript on a WebBrowser control you will have to make use of the InvokeScript method. Because the Navigated event doesn’t come back to the UI Thread we will need to make use of the Dispatcher to actually call the trackEvent method.

trackingBrowser.Navigated += (s, args) =>
                                Dispatcher.BeginInvoke(
                                    () => trackingBrowser.InvokeScript("trackEvent",
                                                                       "WindowsPhoneExperiment",
                                                                       "Load",
                                                                       "On Loading of Application"));

For the sake of the experiment I also added a button to the form to track events on button click. The event handler for the button-click looks like this.

private void button1_Click(object sender, RoutedEventArgs e)
{
    trackingBrowser.InvokeScript("trackEvent",
                                 "WindowsPhoneExperiment",
                                 "Click",
                                 "On Click of a Button");
}

That’s all, and now we wait…

Result

It’s very important to view the result. It will take some time before the Google Analytics stats are working. I waited a day before I looked at the stats. Good to see the concept works.

image

Full code

MainPage.xaml

<phoneNavigation:PhoneApplicationPage x:Class="MM.Phone.Experiments.MainPage"
                                      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                                      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                                      xmlns:phoneNavigation="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Navigation"
                                      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                                      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                                      mc:Ignorable="d"
                                      d:DesignWidth="480"
                                      d:DesignHeight="800"
                                      FontFamily="{StaticResource PhoneFontFamilyNormal}"
                                      FontSize="{StaticResource PhoneFontSizeNormal}"
                                      Foreground="{StaticResource PhoneForegroundBrush}"
                                      xmlns:browser="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.WebBrowser"
                                      Loaded="PhoneApplicationPage_Loaded">

    <Grid x:Name="LayoutRoot"
          Background="{StaticResource PhoneBackgroundBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <!--TitleGrid is the name of the application and page title-->
        <Grid x:Name="TitleGrid"
              Grid.Row="0">
            <TextBlock Text="MM Phone Experiments"
                       x:Name="textBlockPageTitle"
                       Style="{StaticResource PhoneTextPageTitle1Style}" />
            <TextBlock Text="Analytics"
                       x:Name="textBlockListTitle"
                       Style="{StaticResource PhoneTextPageTitle2Style}" />
            <Button Content="Button"
                    Height="70"
                    HorizontalAlignment="Left"
                    Margin="320,77,0,0"
                    Name="button1"
                    VerticalAlignment="Top"
                    Width="160"
                    Click="button1_Click" />
        </Grid>

        <!--ContentGrid is empty. Place new content here-->
        <Grid x:Name="ContentGrid"
              Grid.Row="1">
            <browser:WebBrowser x:Name="trackingBrowser"
                                IsScriptEnabled="True"
                                Visibility="Collapsed" />
        </Grid>
    </Grid>

</phoneNavigation:PhoneApplicationPage>

MainPage.xaml.cs

using System;
using System.Windows;
using Microsoft.Phone.Controls;

namespace MM.Phone.Experiments
{
    public partial class MainPage : PhoneApplicationPage
    {
        public MainPage()
        {
            InitializeComponent();

            SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape;

            trackingBrowser.Navigated += (s, args) =>
                                         Dispatcher.BeginInvoke(
                                             () => trackingBrowser.InvokeScript("trackEvent",
                                                                                "WindowsPhoneExperiment",
                                                                                "Load",
                                                                                "On Loading of Application"));
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            trackingBrowser.Navigate(new Uri("http://change-to-your-domain.com/tracking.htm"));
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            trackingBrowser.InvokeScript("trackEvent",
                                         "WindowsPhoneExperiment",
                                         "Click",
                                         "On Click of a Button");
        }
    }
}

tracking.htm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Windows Phone 7 - Application Tracking</title>
    <script type="text/javascript">
        var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
        document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
	</script>
    <script type="text/javascript">
        var pageTracker = _gat._getTracker("UA-xxxxx-x");
        pageTracker._trackPageview();

        function trackEvent(category, action, label) {
            pageTracker._trackEvent(category, action, label);
        }
    </script>
</head>
<body>
</body>
</html>
  • Gravatar Bjorn Backlund June 24th, 2010 at 16:17
    No need for a WebBrowser, see the new Silverlight Analytics Framework ( http://msaf.codeplex.com ) or the Tracking API in the AppStats solution (www.web-analysis.net)
  • Gravatar sebastian holst June 24th, 2010 at 19:33
    The Silverlight Analytics Framework (SAF) works well for capturing "web analytics". In fact, the technology I am about to reference (Runtime Intelligence) is also a full blown plugin to the SAF framework - so when the SAF is called for, the user can select Runtime Intelligence inside Blend if they like.

    However, Runtime Intelligence instrumentation can also be injected into Silverlight assemblies using the community edition of Dotfuscator found inside Visual Studio 2010 (all SKUs but express). Since you mention CodePlex, it is worth noting that through a partnership with PreEmptive (we make both Dotfuscator and Runtime Intelligence), all CodePlex projects have free access to the commercial analytics repository too. Checkout CodePlex's announcement of the service here http://port25.technet.com/archive/2010/05/06/codeplex-users-to-get-free-analytics-data.aspx

    Here is a 90 second channel 9 video on how to use Dotfuscator for injection here - http://channel9.msdn.com/posts/Clint/Docfuscator-Runtime-Intelligence-in-90-seconds/

    What is compelling about this approach is that you can use Expression Blend SAF to configure "web stats" and Visual Studio Dotf. CE to configure feature usage, and monitor physical layers of software OFF OF the Silverlight client (even Azure). ...AND all of the data streams to the same endpoint for analysis.

    ...and yes, it will work on Windows Mobile 7 too.

    Please note, the free version (CE)of Dotfuscator lags the commercial SKU (since we check in our CE version to MSFT WAY early) and so Windows Mobile 7 support must lag a bit too by necessity. Today, the commercial SKU supports all of this now and the CodePlex analytics works fine with CE.
  • Gravatar progg.ru June 27th, 2010 at 16:02
    <strong>Capture usage information of a Windows Phone 7 application using Google Analytics « Mark Monster...</strong>

    Thank you for submitting this cool story - Trackback from progg.ru...
  • Gravatar mich January 16th, 2011 at 04:11
    i found these 2 companies

    mtiks - http://www.mtiks.com
    localytics - http://www.localytics.com
  • Gravatar Mangesh January 31st, 2011 at 09:01
    Hi,
    I want to make use of MSFT Silverlight Analytics framework, to track usage information of my WPF application.
    How to do this? Any pointer will be helpful.
  • Gravatar Doug Rathbone February 15th, 2011 at 12:23
    hey there,

    thought you'd be interested in knowing that i have a c# library that does this natively called GaDotNet.

    it is open source and is on codeplex:

    http://gadotnet.codeplex.com/

    my original post about it

    http://www.diaryofaninja.com/blog/2010/07/11/google-analytics-event-tracking-in-native-code-ndash-now-in-gadotnet-12

    you could probably adapt this for use with the phone
  • Gravatar joseharriaga October 29th, 2011 at 02:16
    I got it to work. You were right: it was the Interactivy assembly.
    Unfortunately, even though this solution is very clean, the Analytics Framework adds up almost two seconds to my application's start up time.
    Because of this, I think I'll implement the browser solution you proposed before instead. :)
  • Gravatar Air Jordan 4 September 10th, 2012 at 08:47
    I got it to work. You were right: it was the Interactivy
  • Gravatar nike blazer May 3rd, 2014 at 02:50
    followed by 3.5 USD per minute thereafter. Two decades after Mandela's release
    nike blazer http://www.gmgb.eu/images/nikeblazerpascher.php
  • Gravatar men in toms May 12th, 2014 at 08:10
    600.00 Hypertrophy (benign) of prostate w/o urinary obstruction and other lower urinary tract symptoms (LUTS)
  • Gravatar Jess June 9th, 2014 at 04:18
    With it you get access to a website that will track the device and show you everywhere it goes.
    Put a working, programmed transponder key in the vehicle, and you can use an auto starter for example.
    Changes can be made to them making the old keys inoperable and useless.
  • The idea of waking up and pressing a button to have your curtains
    draw up in the morning, or an audible command to unlock your front door may seem unnecessary, but
    for wheelchair users it can offer them independence
    and ease of use like nothing else can. You can install your own laminate flooring or use self-stick
    tiles to place on top of the linoleum. One of the best lighting improvements for children's rooms, is adding in directional track lighting.
  • Gravatar Rachel June 25th, 2014 at 03:45
    However, at present, affecting the city public order and stability
    factors still exist in large quantities, and some very serious security situation in the community overall stable situation, the criminal
    cases, high, burglary, robbery and other cases of high potential,
    to a certain extent, affected the security and stability and
    people's sense of security, public order situation is not optimistic.
    It is one of the most basic security elements that you will need
    to protect your home. This facility referred to a monitoring station which is made available
    24 hours a day by fully qualified security persons.
  • Gravatar Hollister Gilet en contrebas rouge July 19th, 2014 at 05:04
    Stress eating also increases your stress in the long run. The stress hormone, cortisol, makes you crave sugary, fatty food. When you eat sugary treats, your blood sugar spikes and then drops causing you to be more irritable.
  • The purpose of using ultrasonic scalers is to Facilitate the removal and disposal of agents All which causes inflammation to the teeth, plaque and its products, as well as calculus. The term &quot;calculus&quot; is used to Refer to a kind of dental plaque That HAS hardened over time. This requirement is Caused by the accumulation consist of saliva on the plate, and All which already May be Deposited on the teeth. With the rough area Providing a favorable environment for the formation of Increased calculus, dental ultrasonic scalers make for a viable choice of equipment fit for use in Eliminating this problem.
  • Gravatar Freya August 12th, 2014 at 19:19
    This blog was... how do you say it? Relevant!!
    Finally I have found something that helped me.
    Appreciate it!
  • F眉r jeden Erwachsenen, der ein Kind auf einem Shopping-Trip nach Abercrombie nicht getroffen hat, wird es eine Retail-Erfahrung anders als alle anderen sein. Nachdem Sie geduldig ertragen die Warteschlange immer pr盲sent, wo Wartezeiten von bis zu einer Stunde keine Seltenheit sind, bereit sein, f眉r einen Angriff auf die Sinne 脛onen 眉ber Ihre letzten Abenteuer auf Verkauf &quot;Tag in Arnott oder BT.
  • Gravatar basket nike free run August 19th, 2014 at 18:08
    http://nikefreerunhomme.sunnysidefamily.com/nike free run homme
    basket nike free run http://basketnikefreerunpascher.truespiritualhealing.com/
  • Gravatar chaussures nike free run 2015 August 19th, 2014 at 18:08
    http://nikefreerunhomme.sunnysidefamily.com/chaussures nike free run homme
    chaussures nike free run 2015 http://nikefreerun2015.ninjawebdevelopers.net/
  • Gravatar chaussures nike free run August 19th, 2014 at 18:09
    http://nikefreerun2015.ninjawebdevelopers.net/nike free run 2015
    chaussures nike free run http://chaussuresnikefreerun2014.cnc-cmr.org/
  • Gravatar chaussures nike free run homme August 19th, 2014 at 18:09
    http://chaussuresnikefreerunfrance.7vices.org/chaussures nike free run france
    chaussures nike free run homme http://nikefreerunhomme.sunnysidefamily.com/
  • Gravatar femme chaussures nike free August 19th, 2014 at 18:09
    http://chaussuresnikefreerun.getglueid.org/chaussures nike free run
    femme chaussures nike free http://nikefreerunfemme.putthemouthonem.com/
  • Gravatar nike free run france August 19th, 2014 at 18:09
    http://chaussuresnikefreerun.himalayancharities.net/chaussures nike free run pas cher
    nike free run france http://nikefreerunfrance.njseinstitute.org/
  • Gravatar chaussures nike free run france August 19th, 2014 at 18:09
    http://chaussuresnikefreerunfrance.7vices.org/chaussures nike free run
    chaussures nike free run france http://chaussuresnikefreerunfrance.7vices.org/
  • Gravatar chaussures nike free run August 19th, 2014 at 18:09
    http://nikefreerunfemme.putthemouthonem.com/femme chaussures nike free
    chaussures nike free run http://chaussuresnikefreerunpascher.8gsf.org/
  • Gravatar comptoir granite ou quartz Quebec August 24th, 2014 at 12:23
    I am now not certain where you're getting your information, but good topic.

    I needs to spend a while finding out much more or working out
    more. Thank you for excellent info I was searching for this info for my mission.
  • Gravatar zappos gucci loafers September 21st, 2014 at 12:27
    WaMu Theater at Madison Square Garden
  • I've been reading your weblog for a long time now and finally got the bravery to go ahead and give you a shout out from Atascocita Texas.
  • But some of them are legitimate while the others are scams.
  • Gravatar prada occhiali da sole September 21st, 2014 at 13:12
    Le ween end ou quand je suis en vacance on lui ouvre le jardin toute la journée de temps en temps il va dans la rue mais la plupart du temps il est pas trés loin et quand je l'apelle il arrive.
  • Gravatar gucci outlet italy September 23rd, 2014 at 07:46
    Other signs read
  • Gravatar michael kors women's mk5038 September 23rd, 2014 at 09:22
    Donc finalement si je ne vais pas le voir maaintenant, je louerai le film dans 6 mois.
  • Gravatar mcm handbags dallas September 23rd, 2014 at 13:40
    Once you've opted to procure your items the next question arises that from where to shop. Which store should you go create all most. Normally finding the best store for cheaper procurement becomes tough at traditional shopping sites centers. Really it is tough to visit various shops personally and compare there prices for better process. However often you are informed about such offers through banners or advertisement but filth to collect come to ensured yourself as whether it is best one offer anyone personally. But it the internet which remove these hassles and paves way to visit associated with retailers sometimes. You never have to go here personally and within a couple of seconds you easily come get the best quote.
  • Gravatar hermes birkin green ostrich price September 24th, 2014 at 16:07
    La quarta di copertina. A due passi da New York esiste una localit chiamata Logan Beach. D'estate una frequentata stazione di villeggiatura, ma d'inverno ha un'atmosfera da vecchia America dimenticata, angosciosa e rarefatta: un litorale battuto dal vento e dalla pioggia, una grande casa in cima a una scogliera, una villetta sulla sabbia e il resto del mondo lontano come non mai. Proprio qui, e proprio in un inverno pi inclemente del solito, si rifugiano David ed Ellen, una matura coppia di coniugi, con la speranza di ricucire in qualche modo il loro rapporto in crisi da anni, lontani dall'assolata California e da una storia di infedelt che Ellen non riesce a perdonare. Ma l'insidia dietro l'angolo, nella persona di un'enigmatica e bellissima ragazza che compare all'improvviso dal nulla e che pian piano si insinua nella vita di David fino a sconvolgerla, facendo leva sulle sue debolezze. Chi questa donna di nome Marianna? Da dove viene? E soprattutto, cosa vuole? David dovr affrontare e sconfiggere i propri demoni, anche quelli pi inconfessati: in ballo non c' pi soltanto il suo matrimonio, ma la sua sanit mentale e la sua stessa vita.I film in uscita nelle sale in Italia da mercoledì 20
  • Gravatar findvideoservice.com September 24th, 2014 at 18:20
    http://athomeandmain.comathomeandmain.com
  • Gravatar コーチ September 25th, 2014 at 06:38
    コーチ neuf très très beau en compagnie de pochette en tenant aide -livraison dans les instant
  • Gravatar ray ban wayfarer preis deutschland September 25th, 2014 at 15:08
    "Grandfather, I have prayed many times and have gone over every inch of the cornfield. Why doesn't Heavenly Father show me where it is?"
  • Gravatar cheap replica chanel 2.55 September 26th, 2014 at 02:28
    Pristine item!! Thanks so much, can't wait to use it!!!!!!
  • Gravatar domain September 30th, 2014 at 15:02
    Since the admin of this website is working, no uncertainty very soon it will be renowned, due to its feature contents.
Gravatar