Thursday 2 May 2013

Windows Store App - Online Advertising in your app


1. Download the Microsoft Advertising SDK for Windows 8
2. Add references in your project

3. Add refernce in your xaml
xmlns:UI="using:Microsoft.Advertising.WinRT.UI"
4. Add the control in your xaml, make sure you give the correct ApplicationID and AdUnitID created at the pubCentre against your hotmail account. give the correct width and height.
<UI:AdControl Visibility="Visible"  Width="500" Height="65"
                    x:Name="AdCntrl"
                  IsAutoRefreshEnabled="True"   ApplicationId="d25517cb-12d4-4699-8bdc-52040c712cab"  AdUnitId="10042999"
                  />
5. Build the project and run your app. It works!!!

Windows Store App - Google Analytics

1. Download the library from http://w8ga.codeplex.com/releases/view/98265
2. Add reference to it from your app
3. Add the project DT.GoogleAnalytics.Metro in your project as in screen shot


4. Add using DT.GoogleAnalytics.Metro; in the required page in ur app
5. Add the following 2 lines to ur App.xaml, give the exact WebPropertyId - This is very important.

6. Follow the steps  from 3 to 8 in https://w8ga.codeplex.com/wikipage?title=Howto  if u wish to add the GA ref in ur App.xaml.cs 
7. if u want google analytics to work in other pages, add the code below : 

var ver = Windows.ApplicationModel.Package.Current.Id.Version;
var appVersion = ver.Major + "." + ver.Minor + "." + ver.Build + "." + ver.Revision;
//appVersion gives you the version of ur app
string pageUrl = "GAME_SS_" + appVersion.ToString() + "_" + CultureInfo.CurrentCulture.Name + "Win8";
AnalyticsHelper.TrackPageView(pageUrl);
You can give whatever string u want in pageUrl which lets u identify the page of ur app and this url will be displayed in the Google Analytics page.


Windows Store App - Audio Playback across all pages


Add the following in MainPage.xaml

<Frame x:Name="frame1" />
        <MediaElement Name="media"
              AudioCategory="BackgroundCapableMedia"
              Source="/Sounds/bgmusic_v9.mp3" Loaded="OnMediaLoaded"/>

Add the following in MainPage.xaml.cs

private void OnMediaLoaded(object sender, RoutedEventArgs e)
    {
            if (App.appVariables.GlobalAudioElement == null)
              App.appVariables.GlobalAudioElement = sender as MediaElement;
    }



Add a static variable GlobalAudioElement.
public  MediaElement GlobalAudioElement = null;



Add the code in the required pages in ur app

        /// <summary>
        /// Mute the sound and displays the unmute button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnMuteClick(object sender, RoutedEventArgs e)
        {
            App.appVariables.Mute = true;
            App.appVariables.GlobalAudioElement.Pause();
            btnMute.Visibility = Visibility.Collapsed;
            btnUnMute.Visibility = Visibility.Visible;
        }
        /// <summary>
        /// Unmute the sound and displays the mute button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnUnMuteClick(object sender, RoutedEventArgs e)
        {
            App.appVariables.Mute = false;
            App.appVariables.GlobalAudioElement.Play();
            btnMute.Visibility = Visibility.Visible;
            btnUnMute.Visibility = Visibility.Collapsed;
        }