windows phone7中如何使用Google Analytics統計分析行動裝置 App

來源:互聯網
上載者:User

當我們把應用上線到商店後,怎麼知道應用的使用方式呢?當然像AppStore或者Marketplace都會給會員一個後台,登入後可以看到應用的下載量和安裝量,不過好像統計的資料基本都是上周的;而且資料統計還可以說的過去,資料分析?當然也有些第三方統計分析工具;Google Analytics或許大家已經不陌生了,它主要提供免費的企業網站分析服務,統計分析網站流量以及各種推廣效果;可以多維度查看統計報告;當然它本身也提供了移動Tracing Service(這個具體如何使用,本人還未學習);我們能否使用它來幫我們統計行動裝置 App嗎?答案當然是肯定得;

 

前言

在windows phone7中使用Google Analytics,前提是要基於MSAF(Microsoft Silverlight Analytics Framework)架構,這個架構是微軟提供的對 Microsoft Silverlight, WPF, Windows Phone 7的web統計架構;架構對各平台的分析支援如下:

大家可以另去研究學習這個架構http://msaf.codeplex.com/;當你下載源碼後就可以看到對應wp7的solution,在solution裡可以看到Sample

同時到DownLoad選項可以下載SL5&WP7 Sample了,很高興的是這裡面的WP7 Sample就是使用Google Analytics了,而且提供了封裝好了的Google.WebAnalytics.dll,開心下吧,你可以使用Reflector看這裡的代碼,裡面已經封裝了對Google Analytics的介面的調用了;

主要實現是Analytics(包括GoogleAnalytics、ConsoleAnalytics)基於Behavior,然後其他使用者行為(比如點擊按鈕、滾動全景視圖、切換樞軸控制項等)基於觸發器(TriggerAction);這樣在需要捕獲使用者操作的地方添加對應控制項的Triggers的Action就可以了;

對於應用的啟動、關閉、啟用這些在Microsoft.WebAnalytics.WebAnalyticsService中捕獲Microsoft.Phone.Shell.PhoneApplicationService的Activated、Closing、Deactivated、Launching的事件;並在事件裡由資料收集器DataCollector寫或者暫存資料,最後儲存到記錄檔或者遠程平台(Google Analytics);

對於頁面訪問統計,則是在Microsoft.WebAnalytics.WebAnalyticsService 中捕獲PhoneApplicationFrame 的Navigated事件,然後由資料收集器記錄資料;

以上是對MSAF for wp7的淺層次理解,用以協助大家理解;

使用之前需要到http://www.google.com/analytics/申請賬戶或者開通功能,因為應用中需要用於統計分析的Google Analytics賬戶裡的跟蹤ID;

一、找到需要引用的dll,從MSAF wp7的TestApp.wp7項目的Bin目錄裡拷貝出這些dll檔案

        

二、建立Windows Phone7項目

三、添加dll引用;在GoogleAnalytics4Wp7項目解決方案同級目錄裡建立Lib檔案夾,將一中的dll複製到Lib中,然後在GoogleAnalytics4Wp7項目添加對這些dll的引用

四、添加AnalyticsService類啟用MSAF 

View Code

using System;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Ink;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using Google.WebAnalytics;using Microsoft.WebAnalytics;using System.ComponentModel.Composition.Hosting;using Microsoft.WebAnalytics.Behaviors;namespace GoogleAnalytics4Wp7{    public class AnalyticsService : IApplicationService    {        #region IApplicationService Members        public void StartService(ApplicationServiceContext context)        {            CompositionHost.Initialize(                new AssemblyCatalog(Application.Current.GetType().Assembly),                new AssemblyCatalog(typeof(Microsoft.WebAnalytics.AnalyticsEvent).Assembly),                new AssemblyCatalog(typeof(Microsoft.WebAnalytics.Behaviors.TrackAction).Assembly),                new AssemblyCatalog(typeof(GoogleAnalytics).Assembly));        }        public void StopService()        {        }        #endregion    }    }

 

五、App.xaml裡初始化AnalyticsService 

View Code

<Application     x:Class="GoogleAnalytics4Wp7.App"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"             xmlns:mwa="clr-namespace:Microsoft.WebAnalytics;assembly=Microsoft.WebAnalytics"             xmlns:ga="clr-namespace:Google.WebAnalytics;assembly=Google.WebAnalytics"    xmlns:local="clr-namespace:GoogleAnalytics4Wp7"    >    <!--Application Resources-->    <Application.Resources>    </Application.Resources>    <Application.ApplicationLifetimeObjects>        <!--Required object that handles lifetime events for the application-->        <shell:PhoneApplicationService             Launching="Application_Launching" Closing="Application_Closing"             Activated="Application_Activated" Deactivated="Application_Deactivated"/>        <local:AnalyticsService></local:AnalyticsService>             <mwa:WebAnalyticsService>            <mwa:WebAnalyticsService.Services>                <ga:GoogleAnalytics WebPropertyId="" Category="WP75" />            </mwa:WebAnalyticsService.Services>        </mwa:WebAnalyticsService>    </Application.ApplicationLifetimeObjects></Application>

 

六、添加頁面間導航;在Mainpage裡添加按鈕並在它的Click事件裡導航到新的TestPage.xaml頁面;

 

 private void button1_Click(object sender, RoutedEventArgs e)        {            this.NavigationService.Navigate(new Uri("/TestPage.xaml", UriKind.Relative));        }

 

運行程式,點擊按鈕並在Google Analytics即時=>內容統計視窗中查看頁面訪問統計結果

以上就是簡單的頁面訪問統計,同時可以到【標準報告】=>【內容】=>【事件】中查看事件統計;

 

添加對事件的統計:

 給MainPage.xaml裡的Button添加觸發器

 <Button Content="Button" Height="72" HorizontalAlignment="Left" Margin="124,117,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click">                <i:Interaction.Triggers>                    <i:EventTrigger EventName="Click">                        <mwab:TrackAction x:Name="Button1" />                    </i:EventTrigger>                </i:Interaction.Triggers>            </Button>

這樣Google Analytics統計結果中的事件中就會有對應的Click 次數的統計了;

其他各控制項事件的統計也是基於觸發器,這個在開始就已經介紹了;

 

有了統計結果,在Google Analytics報表中就可以即時看到當前使用應用的人數,統計使用者啟動、關閉次數,通過頁面訪問次數可以查看使用者使用習慣;

對你的應用是否有協助呢?  :)

 

執行個體代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.