It is quite common that you may have a Welcome Flow which issues a X% discount to users who have just signed up to your newsletter.
Klaviyo can automatically assign a coupon code to that profile (using something like {% coupon_code 'Welcome-10' %}
), however you are only able to reference this coupon code in the flow.
You might want to remind users they have this coupon and include it in a campaign email, or any other flow.
In order to do this, we need to make sure we store it as a profile property so it can be referenced in any email.
1) Store coupon information as a profile property
Klaviyo has a handy “Coupon Assigned” profile property which is recorded on a profile every time a coupon a code is assigned to a profile. Using this event, a flow and a webhook we can automatically store coupon information as profile properties
Create a flow
Create a new flow that is triggered by the “Coupon Assigned” metric/event
Next, add a “Webhook” action into the flow
Configure Webhook
A webhook lets us make an API call from Klaviyo to any supported API endpoint – that includes Klaviyo’s own API!
We are going to make a webhook call from this Klaviyo Flow, to Klaviyo’s Create or Update Profile api. This will then update the profile with the coupon information.
Create a Private API Key
Follow Klaviyo’s documentation to create a private API key with the profiles:write
scope and make sure you copy/download the key. We will need it shortly.
Populate the Webhook
We will take the information from Klaviyo’s API documentation and use this to populate the webhook details
The first part of the webhook should look like this when complete
(enter your API key in the Authorization
box following the format Klaviyo-API-Key {YOUR_PRIVATE_KEY}
)
The last piece now is to fill in the JSON body of the webhook with the information needed to update the Klaviyo Profile Properties.
Copy and paste the below code into the JSON body section
{
"data": {
"type": "profile",
"id": "{{person.KlaviyoID}}",
"attributes": {
"properties": {
"{{ event.CouponKey}}_CouponExpirationDate": "{{ event.CouponExpirationDate }}",
"{{ event.CouponKey}}_Assigned": "True",
"{{ event.CouponKey}}_CouponCode": "{{ event.UniqueCode }}"
}
}
}
}
Code language: JSON / JSON with Comments (json)
Save and then preview your webhook
If everything is correct, you should see a preview like this
and when you click “Send Test Request” you should get a green success notification. Now go check the profile, and you should see 3 new profile properties have been added
Set Flow Live
We’ve confirmed the webhook is working, so now you can turn the flow live!
Note: This will only apply to profiles who have coupon codes assigned to them from when you set the flow live. It is not possible to apply this logic retroactively.
2) Using coupon information in a campaign
Now that we have successfully added the coupon code information to a profile, we can reference it in any email template.
We also stored the expiry date too, so we can check if the coupon is valid
Here’s an example block where we reference the Welcome Code (our coupon name is Welcome_10
) in the email using {{person|lookup:'Welcome_10_CouponCode'}}
We also make sure we only show this block to users who we know were assigned the coupon code by adding some show/hide display logic. We add in {{person|lookup:'Welcome_10_Assigned'}}
which will return True if the coupon was assigned, and cause the pink block to show. Otherwise it will hide.
Only show coupon if within expiration date
We can go one step further and check that the coupon hasn’t expired either, by checking the date. This requires some more advanced django logic, so is best done in a text block directly instead of using the show/hide logic box
The code for this block looks like this
<div>{% today "%d/%m/%Y" as now %}</div>
{% if person|lookup:'Welcome_10_CouponExpirationDate' > now %}
<div>[CONTENT ON USING COUPON GOES HERE] Use coupon {{person|lookup:'Welcome_10_CouponCode'}}</div>
{% else %}
<div>[THIS PROFILE HAS NO COUPON ASSIGNED - SHOW OTHER CONTENT]</div>
{% endif %}
Code language: HTML, XML (xml)
And will show different content depending on the user