Crownpeak
  • Product Discovery Developer Guide
  • 🛒Item catalog management
    • What is the Items API?
    • How to work with Items
      • Item Schema
        • Attributes
        • Nested Item Schemas
        • Using the Item Schema API
        • DefaultLocale API
        • Onboarding on/migrating to Fredhopper
        • List of Reserved Attributes
      • Category Tree
        • Using the Category Tree API
        • Onboarding on XO
      • Item Catalog
        • Using the Catalog API
      • Items
        • Using the streaming Items API
        • Using the batch Items API
    • Step by Step guide
      • Step by step guide for Fredhopper customers
    • Feedback
      • Using the Feedback API
    • Authorization to APIs
    • Troubleshooting API errors
  • 🎯XO Recommendations
    • Introduction
    • Using the Recommendations API
    • Setting up the Chrome extension
    • Micro-segmentation
    • XO Legacy APIs
  • 🔎XO Search
    • Introduction
    • Getting started
    • API Reference
      • Search API
      • Autocomplete API (Beta)
      • Product Suggest API
    • API Parameters
      • Search
      • Pagination
      • Faceting
      • Sorting
      • Grouping
      • Filtering
      • Disable features
      • Response mask
      • Context
    • Configuration limitation
  • 🧪A/B testing
    • Fredhopper A/B testing
      • Integration steps for a non-caching solution
      • Integration steps for a caching solution
        • Java SDK Integration
          • Setup
          • Retrieve running A/B tests - Java SDK
          • Filter and request variant - Java SDK
          • Extending the SDK
        • Manual A/B tests integration
          • Retrieve running A/B tests
          • Filter out irrelevant A/B tests
          • Assign variants to user
          • Request variant for page
        • Limitations and Best Practices
  • 📚Resources
    • Glossary
    • Best Practices
      • Tracker Best Practices
      • Items API Best Practices
      • Fredhopper Data Configuration Best Practices
      • Fredhopper Query Response Best Practices
      • Fredhopper Merchandising Studio Best Practices
    • Privacy Notice
  • Archived Pages
    • FHR Tracking plan
    • XO Tracking plan
    • The Tracking API and JS Library
      • What to Track
        • Generic Actions
          • View
          • Click
          • Add to Cart
          • Remove from Cart
          • Purchase
        • Custom Actions
      • Initializing the JavaScript Library
      • REST API Technical Documentation
Powered by GitBook
On this page
  • Example using java.net.URI
  • Example using java.util.Map for query params
  1. A/B testing
  2. Fredhopper A/B testing
  3. Integration steps for a caching solution
  4. Java SDK Integration

Filter and request variant - Java SDK

PreviousRetrieve running A/B tests - Java SDKNextExtending the SDK

Last updated 3 years ago

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 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 .

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.

🧪
Overview
Overview