Tracking Ad Clicks with the Microsoft Silverlight Analytics Framework on the Windows Phone

Along with the release of the Windows Phone Developer Tools and the Microsoft Silverlight Analytics Framework 1.46 today, Microsoft also released the Mobile Advertising SDK for Windows Phone.  The SDK consists of a custom control AdControl that you add to your Silverlight application on the phone.  Because the AdControl exposes an AdEngaged event whenever the user clicks on an ad, it is very easy to make the Analytics Framework work with the control.  I plan on adding another assembly to the next build of the Analytics Framework to support this.  Here is the code that is necessary for it if you don’t want to wait.

To use this code:

  1. Add the AdControl to your application as described in the SDK
  2. Add the HandleadControl.cs code to your project
  3. Add a TrackAction behavior to the AdControl and select the AdEngaged event
  4. Now the AdControl.AdUnitId will be tracked in the AnalyticsEvent.ActionValue

HandleAdControl.cs

 namespace Microsoft.WebAnalytics.Advertising
{
    using System;
    using System.ComponentModel.Composition;
    using Microsoft.Advertising.Mobile.UI;
    using Microsoft.WebAnalytics;
    using Microsoft.WebAnalytics.Contracts;

    /// <summary>
    /// Track a Microsoft Advertising SDK for Windows Phone 7 AdControl.AdEngaged event
    /// </summary>
    /// <remarks>
    /// Platforms: Windows Phone 7
    /// </remarks>
    [Tracker(typeof(AdControl))]
    public class HandleAdControl : ITrackHandler
    {
        #region Properties
        /// <summary>
        /// Gets or sets the event Logging method
        /// </summary>
        [Import("Log")]
        public Action<AnalyticsEvent> Log { get; set; }
        #endregion

        #region ITrackAction Members

        /// <summary>
        /// Track the DatePicker ValueChanged event
        /// </summary>
        /// <param name="associatedObject">an AdControl object</param>
        /// <param name="analyticsEvent">the analytics event</param>
        /// <param name="eventArgs">the event arguments</param>
        public void Track(
            System.Windows.FrameworkElement associatedObject,
            AnalyticsEvent analyticsEvent,
            System.EventArgs eventArgs)
        {
            var adControl = associatedObject as AdControl;

            if (analyticsEvent.Name == "AdEngaged")
            {
                var args = eventArgs as EventArgs;

                analyticsEvent.ActionValue = adControl.AdUnitId;
            }

            this.Log(analyticsEvent);
        }

        #endregion
    }
}