Filter and request variant - Java SDK

Once the AbTesting object has been created using the builder and started, the running AB tests will become available to you. For this purpose you need to ensure that the start method has been called before moving on.

If you refer to the Overview section, you will see that there's a few steps to fully implement a working A/B tests solution. Aside from the retrieval which is done automatically by calling the start() method, the SDK also neatly packs the filtering and variant assignment through the appendAbTestsParameter() method, by appending the fh_abtests parameter automatically to your FHR query of choice.

This should be called once per web request to be able to perform the calculation for the respective shopper on a respective page.

Important! The appendAbTestsParameter method does quite a bit of work, and if you are unsure about what goes under the hood you can always refer to the separate steps mentioned in the Overview.

The appendAbTestsParameter method can be used either with a URI or a Map.

Example using java.net.URI

One way to make use of the appendAbTestsParameter() method is to use a URI. Consider the example below:

import java.net.URI;
...

abTesting.start();


// Update an FHR query for a location with universe catalog01, locale en_AU, search terms dress
URI uri = URI.create("http://{baseUrl}/fredhopper/query?fh_location=//catalog01/en_AU/$s=dress");

// The session id of the user
String sessionId = "8ba07e4f-3e29-5f6b-845f-fc4a498028e7";

URI uriWithParameters = abTesting.appendAbTestsParameter(sessionId, uri);

Running the above snippet will do the following steps:

  • The retrieval will start, when the start() method is called. The running A/B tests will be stored in the built-in cache.

  • The appendAbTestsParameter(sessionId, uri) will then:

    • Get the running A/B tests from the cache.

    • Filter out the unnecessary tests for the page provided based on the query parameters and the filters present in the running A/B tests

    • Generate a selection of A/B test and variant combinations, leveraging the sessionId.

    • Combine the selection of A/B test and variant combinations in the necessary format, and append it to the URI as the fh_abtests parameter.

Once complete, you will be able to use the newly generated uriWithParameters to perform your FHR query as you normally would, which would contain variants for all the running A/B tests on that page, specific for the user, based on the session id.

Example using java.util.Map for query params

Instead of sending a complete URI you can also opt for sending the query parameters of your FHR query as a hashmap to the appendAbTestsParameter method. Consider the example below:

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
...

abTesting.start();


Map<String, List<String>> queryParams = new Hashmap<>();
queryParams.put("fh_location", Collections.singletonList("//catalog01/en_AU/$s=dress");

String sessionId = "8ba07e4f-3e29-5f6b-845f-fc4a498028e7";

Map<String, List<String>> updatedQuery = abTesting.appendAbTestsParameter(sessionId, queryParams);

Running the above snippet will do the following steps:

  • The retrieval will start, when the start() method is called. The running A/B tests will be stored in the built-in cache.

  • The appendAbTestsParameter(sessionId, queryParams) will then:

    • Get the running A/B tests from the cache.

    • Filter out the unnecessary tests for the page provided based on the query parameters and the filters present in the running A/B tests

    • Generate a selection of A/B test and variant combinations, leveraging the sessionId.

    • Combine the selection of A/B test and variant combinations in the necessary format, and append it to a copy of the queryParams map with fh_abtests as key.

Once complete, you will be able to use the newly generated queryParams map **to perform your FHR query as you normally would, which would contain variants for all the running **A/B tests on that page, specific for the user, based on the_* session id.\_*

Important! In both cases, if no applicable A/B tests are found, the query will remain unchanged.

Last updated