Klaviyo Flow Experiments
Custom Action Setup
Environment Variables
Variable
Description
Outputs
Output
Type
Description
Code
import { Profiles } from 'klaviyo'
const EXPERIMENT_ID = process.env.EXPERIMENT_ID;
const API_KEY = process.env.API_KEY;
export default async (event, profile, context) => {
// construct assignment payload
const payload = {
profile: {
type: profile.data.type,
id: profile.data.id,
attributes: profile.data.attributes,
properties: profile.data.properties
},
event: {
type: event.data.type,
id: event.data.id,
attributes: event.data.attributes
},
context: {
trigger: context.trigger,
function_id: context.function_id
}
};
// fetch assignment
const res = await fetch(
`https://api.aws.neonblue.ai/v1/render/${EXPERIMENT_ID}?user_id=${profile.data.id}`,
{
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'X-Partner': 'coterie',
'X-Api-Key': API_KEY,
'X-Preview': context.trigger.type == "MANUAL_RUN"
},
body: JSON.stringify(payload)
}
);
const assignment = await res.json();
const assignment_id =
assignment.$metadata.assignment_id ||
'00000000-0000-0000-0000-000000000000';
// update user profile with variant content
const $neonblue = profile.data.attributes.$neonblue || {};
$neonblue[assignment_id] = assignment.value;
await Profiles.updateProfile(profile.data.id, {
data: {
type: "profile",
id: profile.data.id,
attributes: {
properties: {
$neonblue: $neonblue
}
}
}
});
return {
assignment_id: assignment_id
};
}How It Works
Template Setup
Subject and Preview Text
Email Body
Template Checklist
Last updated