Code your app for experimentation

After you create a project and define remote variables in Partner Center, you are ready to update the code in your Universal Windows Platform (UWP) app to:

  • Receive remote variable values from Partner Center.
  • Use remote variables to configure app experiences for your users.
  • Log events to Partner Center that indicate when users have viewed your experiment and performed a desired action (also called a conversion).

To add this behavior to your app, you'll use APIs provided by the Microsoft Store Services SDK.

The following sections describe the general process of getting variations for your experiment and logging events to Partner Center. After you code your app for experimentation, you can define an experiment in Partner Center. For a walkthrough that demonstrates the end-to-end process of creating and running an experiment, see Create and run your first experiment with A/B testing.

Note

Some of the experimentation APIs in the Microsoft Store Services SDK use the asynchronous pattern to retrieve data from Partner Center. This means that part of the execution of these methods may take place after the methods are invoked, so your app's UI can remain responsive while the operations complete. The asynchronous pattern requires your app to use the async keyword and await operator when calling the APIs, as demonstrated by the code examples in this article. By convention, asynchronous methods end with Async.

Configure your project

To get started, install the Microsoft Store Services SDK on your development computer and add the necessary references to your project.

  1. Install the Microsoft Store Services SDK.
  2. Open your project in Visual Studio.
  3. In Solution Explorer, expand your project node, right-click References, and click Add Reference.
  4. In Reference Manager, expand Universal Windows and click Extensions.
  5. In the list of SDKs, select the check box next to Microsoft Engagement Framework and click OK.

Note

The code examples in this article assume that your code file has using statements for the System.Threading.Tasks and Microsoft.Services.Store.Engagement namespaces.

Get variation data and log the view event for your experiment

In your project, locate the code for the feature that you want to modify in your experiment. Add code that retrieves data for a variation, use this data to modify the behavior of the feature you are testing, and then log the view event for your experiment to the A/B testing service in Partner Center.

The specific code you need will depend on your app, but the following example demonstrates the basic process. For a complete code example, see Create and run your first experiment with A/B testing.

private StoreServicesExperimentVariation variation;
private StoreServicesCustomEventLogger logger;

// Assign this variable to the project ID for your experiment from Dev Center.
// The project ID shown below is for example purposes only.
private string projectId = "F48AC670-4472-4387-AB7D-D65B095153FB";

private async Task InitializeExperiment()
{
    // Get the current cached variation assignment for the experiment.
    var result = await StoreServicesExperimentVariation.GetCachedVariationAsync(projectId);
    variation = result.ExperimentVariation;

    // Refresh the cached variation assignment if necessary.
    if (result.ErrorCode != StoreServicesEngagementErrorCode.None || result.ExperimentVariation.IsStale)
    {
        result = await StoreServicesExperimentVariation.GetRefreshedVariationAsync(projectId);

        if (result.ErrorCode == StoreServicesEngagementErrorCode.None)
        {
            variation = result.ExperimentVariation;
        }
    }

    // Get the remote variable named "buttonText" and assign the value
    // to the button.
    var buttonText = variation.GetString("buttonText", "Grey Button");
    await button.Dispatcher.RunAsync(
        Windows.UI.Core.CoreDispatcherPriority.Normal,
        () =>
        {
            button.Content = buttonText;
        });

    // Log the view event named "userViewedButton" to Dev Center.
    if (logger == null)
    {
        logger = StoreServicesCustomEventLogger.GetDefault();
    }

    logger.LogForVariation(variation, "userViewedButton");
}

The following steps describe the important parts of this process in detail.

  1. Declare a StoreServicesExperimentVariation object that represents the current variation assignment and a StoreServicesCustomEventLogger object that you will use to log view and conversion events to Partner Center.

    private StoreServicesExperimentVariation variation;
    private StoreServicesCustomEventLogger logger;
    
  2. Declare a string variable that is assigned to the project ID for the experiment you want to retrieve.

    Note

    You obtain a project ID when you create a project in Partner Center. The project ID shown below is for example purposes only.

    private string projectId = "F48AC670-4472-4387-AB7D-D65B095153FB";
    
  3. Get the current cached variation assignment for your experiment by calling the static GetCachedVariationAsync method, and pass the project ID for your experiment to the method. This method returns a StoreServicesExperimentVariationResult object that provides access to the variation assignment via the ExperimentVariation property.

    var result = await StoreServicesExperimentVariation.GetCachedVariationAsync(projectId);
    variation = result.ExperimentVariation;
    
  4. Check the IsStale property to determine whether the cached variation assignment needs to be refreshed with a remote variation assignment from the server. If it does need to be refreshed, call the static GetRefreshedVariationAsync method to check for an updated variation assignment from the server and refresh the local cached variation.

    if (result.ErrorCode != StoreServicesEngagementErrorCode.None || result.ExperimentVariation.IsStale)
    {
        result = await StoreServicesExperimentVariation.GetRefreshedVariationAsync(projectId);
    
        if (result.ErrorCode == StoreServicesEngagementErrorCode.None)
        {
            variation = result.ExperimentVariation;
        }
    }
    
  5. Use the GetBoolean, GetDouble, GetInt32, or GetString methods of the StoreServicesExperimentVariation object to get the values for the variation assignment. In each method, the first parameter is the name of the variation that you want to retrieve (this is the same name of a variation that you enter in Partner Center). The second parameter is the default value that the method should return if it is not able to retrieve the specified value from Partner Center (for example, if there is no network connectivity), and a cached version of the variation is not available.

    The following example uses GetString to get a variable named buttonText and specifies a default variable value of Grey Button.

    var buttonText = variation.GetString("buttonText", "Grey Button");
    
  6. In your code, use the variable values to modify the behavior of the feature you are testing. For example, the following code assigns the buttonText value to the content of a button in your app. This example assumes you have already defined this button elsewhere in your project.

    await button.Dispatcher.RunAsync(
        Windows.UI.Core.CoreDispatcherPriority.Normal,
        () =>
        {
            button.Content = buttonText;
        });
    
  7. Finally, log the view event for your experiment to the A/B testing service in Partner Center. Initialize the logger field to a StoreServicesCustomEventLogger object and call the LogForVariation method. Pass the StoreServicesExperimentVariation object that represents the current variation assignment (this object provides context about the event to Partner Center) and the name of the view event for your experiment. This must match the view event name that you enter for your experiment in Partner Center. Your code should log the view event when the user starts viewing a variation that is part of your experiment.

    The following example shows how to log a view event named userViewedButton. In this example, the goal of the experiment is to get the user to click a button in the app, so the view event is logged after the app has retrieved the variation data (in this case, the button text) and assigned it to the content of the button.

    if (logger == null)
    {
        logger = StoreServicesCustomEventLogger.GetDefault();
    }
    
    logger.LogForVariation(variation, "userViewedButton");
    

Log conversion events to Partner Center

Next, add code that logs conversion events to the A/B testing service in Partner Center. Your code should log a conversion event when the user reaches an objective for your experiment. The specific code you need will depend on your app, but here are the general steps. For a complete code example, see Create and run your first experiment with A/B testing.

  1. In code that runs when the user reaches an objective for one of the goals of the experiment, call the LogForVariation method again and pass the StoreServicesExperimentVariation object and the name of a conversion event for your experiment. This must match one of the conversion event names that you enter for your experiment in Partner Center.

    The following example logs a conversion event named userClickedButton from the Click event handler for a button. In this example, the goal of the experiment is to get the user to click the button.

    private void button_Click(object sender, RoutedEventArgs e)
    {
        if (logger == null)
        {
            logger = StoreServicesCustomEventLogger.GetDefault();
        }
    
        logger.LogForVariation(variation, "userClickedButton");
    }
    

Next steps

After you code the experiment in your app, you are ready for the following steps:

  1. Define your experiment in Partner Center. Create an experiment that defines the view events, conversion events, and unique variations for your A/B test.
  2. Run and manage your experiment in Partner Center.