Skip to content

Conversation

petzel
Copy link
Contributor

@petzel petzel commented Aug 9, 2023

Motivation and Context

Customers have requested the ability to ensure a customer continues to receive a treat once assigned, even if the feature flag/experiment has concluded and is no longer assigning any users.

Description

This PR creates hooks which are invoked prior to and after assignment. If the pre assignment hook returns a string, it will be used/returned as the variation. The post assignment hook is a way to handle saving/persistence of assigned variations for a given user.

Example of how this can be used:

const key = `${subject}-my-cool-experiment`;
const variation = await eppoClient.getAssignmentWithHooks(
  'user-123',
  'my-cool-experiment',
  {},
  {
    onPreAssignment(subject: string): Promise<string> {
      const overriddenTreatment = database.get(key);
      if (overriddenTreatment != null && overriddenTreatment !== '') {
        return Promise.resolve(overriddenTreatment);
      }
      return null;
    },

    onPostAssignment(variation: string): Promise<void> {
      database.save(key, variation);
    },
  },
);

@petzel petzel force-pushed the assignment-hooks branch from eddbf5b to dd1ae75 Compare August 9, 2023 15:03
@petzel petzel merged commit 530eea6 into main Aug 12, 2023
@petzel petzel deleted the assignment-hooks branch August 12, 2023 01:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants