lundi 30 novembre 2015

Proof of Concept: Trying to update PubCenter Ad ID and Ad Unit Id without having to resubmit your application

Proof of Concept: What if I wanted to create my own Ad manager that would allow me to be able to change the PubCenter Ad Id and Unit Id without having to resubmit the app.

 Here is the main idea behind this POC:
  • The different steps, first we will create a json file that will hold our default information.
  • Next when the application first launch’s it will read this file and store the information to memory.
  • Afterward we will call an endpoint with an updated version of the XML file and will copy the information of this json into that one if and only if the updated version has a higher version number.
  • Next when the UserControl containing the PubCenter ( or something else) ad is called it will read in memory the different values and pass them to the PubCenter element.
Lets get Coding!!

But first you need some place to store the file that will hold your information, you can either use your own server or dropbox, because I wanted to go quickly for this POC I used dropbox: https://www.dropbox.com/s/ruoe5dknprvn68d/ExampleAds.txt?dl=1

Now we can really start coding

  • You will need to create a file called DefaultAds.txt at the root of your app files, with the following content:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
{
  "AdSettings": {
    "Created" : "05102015",
    "Version" : "1",
    "AdElement": [
      {
        "Type": "small",
        "Kind": "Image",
        "AppID": "fb7f2816-4607-4508-86ee-e5c2058b627e",
        "AdID": "220045"
      },
      {
        "Type": "small",
        "Kind": "Image",
        "AppID": "fb7f2816-4607-4508-86ee-e5c2058b627e",
        "AdID": "220048"
      },
      {
        "Type": "medium",
        "Kind": "Post",
       "AppID": "fb7f2816-4607-4508-86ee-e5c2058b627e",
        "AdID": "220046"
      },
      {
        "Type": "medium",
        "Kind": "Post",
        "AppID": "fb7f2816-4607-4508-86ee-e5c2058b627e",
        "AdID": "220047"
      },
      {
        "Type": "medium",
        "Kind": "Main",
          "AppID": "fb7f2816-4607-4508-86ee-e5c2058b627e",
        "AdID": "220049"
      }
    ]
  }
}
  • Then we are going to create a class named AdPubcenterService in which we will add a method called ReadFileFromStorageFile which will luck as follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
private async static Task ReadFileFromStorageFile()
        {
            var adSettings = new AdSettingsRoot();

            try
            {
                String ResourceReference = "ms-appx:///DefaultAds.txt";
                StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(ResourceReference, UriKind.Absolute));
                Stream src = await file.OpenStreamForReadAsync();

                using (StreamReader sr = new StreamReader(src))
                {
                    string text = sr.ReadToEnd();
                    adSettings = JsonConvert.DeserializeObject<AdSettingsRoot>(text);
                }

                //Updating data
                App.AdSettingsBase.AdElement_Small1 = adSettings.AdSettings.AdElement[0];
                App.AdSettingsBase.AdElement_Small2 = adSettings.AdSettings.AdElement[1];
                App.AdSettingsBase.AdElement_Medium_1 = adSettings.AdSettings.AdElement[2];
                App.AdSettingsBase.AdElement_Medium_2 = adSettings.AdSettings.AdElement[3];
                App.AdSettingsBase.AdElement_Medium_Main = adSettings.AdSettings.AdElement[4];

                App.AdSettingsBase.Version = adSettings.AdSettings.Version;
                App.AdSettingsBase.LastUpdated = Convert.ToDateTime(adSettings.AdSettings.Created);
            }
            catch (Exception e)
            {
                //Catch errors here
            }
        }


  • Now that we can read our file localy we also need to be able to ready our file that is store somewhere on on the web (on dropbox for me).  I will create a method call GetAdsFromHttpJsonFiles which will read our local file first, then make a call to our url and save the data for the url.



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
        public static string PageUrl = "https://www.dropbox.com/s/ruoe5dknprvn68d/ExampleAds.txt?dl=1";

        public static async Task GetAdsFromHttpJsonFiles()
        {
            await ReadFileFromStorageFile();

            AdSettingsRoot adSettings = new AdSettingsRoot();
            bool error = false;

            try
            {
                var req = new HttpClient();
                var message = new HttpRequestMessage(HttpMethod.Get, PageUrl);

                var response = await req.SendAsync(message);
                string result = await response.Content.ReadAsStringAsync();
                adSettings = JsonConvert.DeserializeObject<AdSettingsRoot>(result);
            }
            catch (Exception e)
            {
                error = true;
            }

            ParseElementForAds(adSettings);
        }



  • The Method ParseElementForAds is what allow us to save information that we have store outside our application.  In this method:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
     private static void ParseElementForAds(AdSettingsRoot elementRoot)
        {
            bool updateFile = false;

            //Makign sure the file is not empty
            if (elementRoot.AdSettings != null)
            {
                //if Initialsing file is needed
                if (App.AdSettingsBase == null)
                {
                    App.AdSettingsBase = new AdSettingsBase();
                    updateFile = true;
                }

                //Checking the version
                int version = (int)(elementRoot.AdSettings.Version);

                //if new gotten elements are newer then the one i have update the settings file
                if (!updateFile && App.AdSettingsBase.Version < version)
                {
                    updateFile = true;
                }

                //making sure that we should update the file & that we have the correct count
                if (updateFile && elementRoot.AdSettings.AdElement.Count == 5)
                {
                    App.AdSettingsBase.Version = version;
                    App.AdSettingsBase.LastUpdated = DateTime.Now;

                    App.AdSettingsBase.AdElement_Small1 = elementRoot.AdSettings.AdElement[0];
                    App.AdSettingsBase.AdElement_Small2 = elementRoot.AdSettings.AdElement[1];
                    App.AdSettingsBase.AdElement_Medium_1 = elementRoot.AdSettings.AdElement[2];
                    App.AdSettingsBase.AdElement_Medium_2 = elementRoot.AdSettings.AdElement[3];
                    App.AdSettingsBase.AdElement_Medium_Main = elementRoot.AdSettings.AdElement[4];

#pragma warning disable 4014
                    SaveFileFromStorageFile(elementRoot);
#pragma warning restore 4014

                }
            }
        }
   
  • Next we will create a UserControl and add pubcenter XAML to it:
1
2
3
4
5
6
 
<ui:AdControl x:Name="AdMain"
                  Width="490"
                  Height="80"
                  Margin="0,0,0,0"
                  HorizontalAlignment="Left"
                  ErrorOccurred="AdControl_OnErrorOccurred" />

  • And when the user control is initialized we will pass our AdI d and AppId to the PubCenter Controller:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
  public PublicityUserControl()
        {
            this.InitializeComponent();

            try
            {
                if (App.AdSettingsBase != null)
                {
                    AdMain.AdUnitId = App.AdSettingsBase.AdElement_Medium_1.AdID; //"219859";
                    AdMain.ApplicationId = App.AdSettingsBase.AdElement_Medium_1.AppID; //"45e36de2-ed28-4ca7-9eea-779875b65700"; 
                }
            }
            catch (Exception e)
            {
              
            }
        }

        private void AdControl_OnErrorOccurred(object sender, AdErrorEventArgs e)
        {
            Debug.WriteLine(e.Error);
        }

And there you have it, you can now manage you Ad Id and App Id from a remote location without having to resubmit your application every time!

Here is the source code: https://onedrive.live.com/redir?resid=70A41C6835F56338!39329&authkey=!ANWpJkTH9Bpn5ho&ithint=file%2czip