PageRenderTime 77ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/WP7/Microsoft.WebAnalytics/Microsoft.WebAnalytics/WP7DataCollector.cs

#
C# | 247 lines | 105 code | 36 blank | 106 comment | 4 complexity | b243e95723419f561df16a67d2eee240 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. // <copyright file="WP7DataCollector.cs" company="Microsoft Corporation">
  2. // Copyright (c) 2010 Microsoft Corporation All Rights Reserved
  3. // </copyright>
  4. // <author>Michael S. Scherotter</author>
  5. // <email>mischero@microsoft.com</email>
  6. // <date>2010-07-19</date>
  7. // <summary>Windows Phone 7 Data collector</summary>
  8. namespace Microsoft.WebAnalytics
  9. {
  10. using System;
  11. using System.ComponentModel.Composition;
  12. using System.Globalization;
  13. using System.Windows;
  14. using System.Xml;
  15. using Microsoft.WebAnalytics.Contracts;
  16. /// <summary>
  17. /// Data collector for Windows Phone 7
  18. /// </summary>
  19. [Export(typeof(IDataCollector))]
  20. public class WP7DataCollector : DataCollector, IDisposable
  21. {
  22. #region Fields
  23. /// <summary>
  24. /// The ProductID application from the Windows Phone App Manifest
  25. /// </summary>
  26. private string productId;
  27. #endregion
  28. #region Constructors
  29. /// <summary>
  30. /// Initializes a new instance of the WP7DataCollector class.
  31. /// </summary>
  32. /// <remarks>this constructor is called by MEF</remarks>
  33. public WP7DataCollector()
  34. {
  35. }
  36. #endregion
  37. #region Properties
  38. /// <summary>
  39. /// Gets the App.ProductID from the Windows Phone WMAppManifest
  40. /// </summary>
  41. private string ProductId
  42. {
  43. get
  44. {
  45. if (string.IsNullOrEmpty(this.productId))
  46. {
  47. this.productId = GetAppAttribute("ProductID");
  48. }
  49. return this.productId;
  50. }
  51. }
  52. #endregion
  53. #region Methods
  54. /// <summary>
  55. /// Log an event
  56. /// </summary>
  57. /// <param name="logEvent">the event to log</param>
  58. /// <remarks>This is exported via MEF</remarks>
  59. [Export("Log")]
  60. public new void Log(AnalyticsEvent logEvent)
  61. {
  62. base.Log(logEvent);
  63. }
  64. /// <summary>
  65. /// Dispose of the GeoCoordinateWatcher
  66. /// </summary>
  67. public void Dispose()
  68. {
  69. this.Dispose(true);
  70. GC.SuppressFinalize(this);
  71. }
  72. /// <summary>
  73. /// Dispose of the watcher
  74. /// </summary>
  75. /// <param name="managedAndNative">dispose of both managed and native</param>
  76. protected virtual void Dispose(bool managedAndNative)
  77. {
  78. ////if (this.watcher != null)
  79. ////{
  80. //// this.watcher.Dispose();
  81. //// this.watcher = null;
  82. ////}
  83. }
  84. ////public bool IsLocationTrackingEnabled
  85. ////{
  86. //// get
  87. //// {
  88. //// return this.isLocationTrackingEnabled;
  89. //// }
  90. //// set
  91. //// {
  92. //// this.isLocationTrackingEnabled = value;
  93. //// if (this.isLocationTrackingEnabled)
  94. //// {
  95. //// if (this.watcher == null)
  96. //// {
  97. //// this.watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default)
  98. //// {
  99. //// MovementThreshold = 20
  100. //// };
  101. //// this.watcher.Start();
  102. //// }
  103. //// }
  104. //// else
  105. //// {
  106. //// if (this.watcher != null)
  107. //// {
  108. //// this.watcher.Stop();
  109. //// this.watcher.Dispose();
  110. //// this.watcher = null;
  111. //// }
  112. //// }
  113. //// }
  114. ////}
  115. /// <summary>
  116. /// Initialize the events and properties
  117. /// </summary>
  118. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Object is disposed in Dispose method.")]
  119. protected override void OnInitialize()
  120. {
  121. this.Title = GetAppAttribute("Title");
  122. this.Version = GetAppAttribute("Version");
  123. var root = Application.Current.RootVisual as Microsoft.Phone.Controls.PhoneApplicationFrame;
  124. if (root != null)
  125. {
  126. root.Navigated += new System.Windows.Navigation.NavigatedEventHandler(this.OnRootNavigated);
  127. root.FragmentNavigation += new System.Windows.Navigation.FragmentNavigationEventHandler(this.OnRootFragmentNavigation);
  128. }
  129. }
  130. /// <summary>
  131. /// attach phone-specific data to each event
  132. /// </summary>
  133. /// <param name="logEvent">the logEvent</param>
  134. protected override void OnLog(AnalyticsEvent logEvent)
  135. {
  136. logEvent.EventArgs.Add(new Microsoft.WebAnalytics.Data.PropertyValue { PropertyName = "ProductID", Value = this.ProductId });
  137. logEvent.DocumentUri = new Uri(this.Title, UriKind.Relative);
  138. ////if (this.IsLocationTrackingEnabled)
  139. ////{
  140. //// if (this.watcher == null)
  141. //// {
  142. //// this.OnInitialize();
  143. //// }
  144. //// if (this.watcher.Status == GeoPositionStatus.Ready)
  145. //// {
  146. //// var location = string.Format(
  147. //// CultureInfo.InvariantCulture,
  148. //// "{0},{1}",
  149. //// this.watcher.Position.Location.Latitude,
  150. //// this.watcher.Position.Location.Longitude);
  151. //// logEvent.EventArgs.Add(new Data.PropertyValue { PropertyName = "Location", Value = location });
  152. //// }
  153. ////}
  154. }
  155. #endregion
  156. #region Implementation
  157. /// <summary>
  158. /// Gets an attribute from the Windows Phone App Manifest App element
  159. /// </summary>
  160. /// <param name="attributeName">the attribute name</param>
  161. /// <returns>the attribute value</returns>
  162. private static string GetAppAttribute(string attributeName)
  163. {
  164. string appManifestName = "WMAppManifest.xml";
  165. string appNodeName = "App";
  166. var settings = new XmlReaderSettings();
  167. settings.XmlResolver = new XmlXapResolver();
  168. using (XmlReader rdr = XmlReader.Create(appManifestName, settings))
  169. {
  170. rdr.ReadToDescendant(appNodeName);
  171. if (!rdr.IsStartElement())
  172. {
  173. throw new System.FormatException(appManifestName + " is missing " + appNodeName);
  174. }
  175. return rdr.GetAttribute(attributeName);
  176. }
  177. }
  178. /// <summary>
  179. /// Root Fragment Navigation event handler
  180. /// </summary>
  181. /// <param name="sender">the NavigationService</param>
  182. /// <param name="e">the fragment navigation event arguments</param>
  183. private void OnRootFragmentNavigation(object sender, System.Windows.Navigation.FragmentNavigationEventArgs e)
  184. {
  185. var logEvent = new AnalyticsEvent
  186. {
  187. HitType = Data.HitType.PageView,
  188. Name = "FragmentNavigation",
  189. ObjectType = sender.GetType().Name
  190. };
  191. this.Log(logEvent);
  192. }
  193. /// <summary>
  194. /// Root Navigated event handler
  195. /// </summary>
  196. /// <param name="sender"> the NavigationService</param>
  197. /// <param name="e">the navigation event arguments</param>
  198. private void OnRootNavigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
  199. {
  200. var logEvent = new AnalyticsEvent
  201. {
  202. ActionValue = e.Uri.ToString(),
  203. HitType = Data.HitType.PageView,
  204. Name = "Navigated",
  205. ObjectType = sender.GetType().Name
  206. };
  207. this.Log(logEvent);
  208. }
  209. #endregion
  210. }
  211. }