Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assignment hooks and asynchronous method to enable sticky assignments #2

Merged
merged 3 commits into from
Aug 12, 2023

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 merged commit 530eea6 into main Aug 12, 2023
2 checks passed
@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
3 participants