A Matcher in deco.cx is a function that returns a boolean.
Matcher | Common Use Cases | Sticky |
---|---|---|
Random Matcher | A/B test your page with 50% of traffic. | session |
Cron Matcher | Change a page every Friday between 10 AM and 11 AM. | none |
Date Matcher | Create a Black Friday page / Schedule banners to appear at specific dates and times. | none |
Device Matcher | Show a different page based on the user’s device. | none |
matchers
folder, similar
to sections and loaders. Let’s create a new matcher called MyMatcher.ts
to
illustrate the process.
The signature of our matcher will follow this structure:
MyMatcher
function accepts props
as the input,
allowing you to pass any necessary data to the matcher. Additionally, it
receives a ctx
object of type MatchContext
, which contains the request
information. You have the flexibility to perform any desired operations within
the matcher function and return a boolean value based on the evaluation.
Let’s take a look at the MatchDate
example from the deco library:
MatchDate
function serves as a Matcher. It accepts
props
as input, which includes start
and end
properties. The function
evaluates whether the current date falls within the specified date range. If no
start
or end
value is provided, it defaults to true
. The MatchDate
function returns a boolean value based on the evaluation.
Matchers can also have a “sticky” behavior, which is particularly useful for A/B
testing scenarios. To make a Matcher sticky on the user’s session, you can
export a constant named sticky
with the value "session"
, as shown below:
MatchRandom
implementation using the sticky session
feature:
MatchRandom
example, the matcher function MatchRandom
accepts
traffic
as a prop, representing the percentage of traffic that should match
the condition. By generating a random number between 0 and 1, the function
determines whether the generated value is less than the specified traffic
percentage. The Matcher returns true
or false
based on this evaluation.
Matchers provide great flexibility to customize and extend deco’s functionality
to meet your specific needs. With the ability to create custom Matchers, you can
integrate external data sources, perform complex calculations, and implement
intricate logic to determine user segmentation and deliver personalized
experiences.