Skip to main content
Solved

Custom variable assignment


Forum|alt.badge.img+1

Is there a way to assign a custom variable in an email template? Here’s the scenario...

I have an event property that is a URL with a structure like this: https:test.com/customer/CUSTOMERID/event/EVENTID

I want to set the CUSTOMERID and EVENTID to separate variables. I understand that we can split the URL by forward slash like this: {{ event.URL | split: ‘/’ }} but that just prints an array.

I’d like to do something like:

{% url_split = event.URL | split: '/' %}
{% c_id = url_split[4] %}
{% e_id = url_split[6] %}
Customer ID: {{ c_id }}
Event ID: {{ e_id }}

Is this possible? I’ve also tried using a Python custom action before the email but I can’t figure out how to pass the variable to the email.

Thanks all.

 

Best answer by MANSIR2094

  ​@robdentonrachio   I appreciate your insights on the lookup filter—you're absolutely right, and that’s a great approach. Thanks for sharing that!

Regarding your question about passing variables in the Klaviyo event payload, I recently completed a similar project for another client and can definitely help you with this. The key is ensuring that the event properties are structured correctly before sending the payload.

Here’s the recommended structure:

event_payload = {
    "data": {
        "type": "event",
        "attributes": {
            "event_name": "Your Event Name",
            "event_properties": {
                "CUSTOMERID": CUSTOMERID,
                "EVENTID": EVENTID
            }
        }
    }
}

Make sure the request is properly formatted when sending the event to Klaviyo. If you’re using a POST request to Klaviyo’s API, ensure that the content type is set to application/json and that the payload is correctly encoded.

If you're still running into issues, I’d be happy to take a closer look and assist further. Let me know how you'd like to proceed!

View original
Did this topic or the replies in the thread help you find an answer to your question?

5 replies

MANSIR2094
Problem Solver IV
Forum|alt.badge.img+17
  • Problem Solver IV
  • 241 replies
  • February 21, 2025

Hello ​@robdentonrachio

Klaviyo’s template language (based on Django’s Liquid) does not support direct variable assignment using {% %} tags. However, you can extract values from the split array inline using {{ }}.

Try this:

Customer ID: {{ event.URL | split: '/' | slice: 4 }}  
Event ID: {{ event.URL | split: '/' | slice: 6 }}

This extracts the 5th (CUSTOMERID) and 7th (EVENTID) elements from the split array.

If you're using a Python custom action, you’ll need to process the URL in Python and pass CUSTOMERID and EVENTID as separate properties in the Klaviyo event payload, then reference them in the email as {{ event.CUSTOMERID }} and {{ event.EVENTID }}.


Forum|alt.badge.img+1

Thank you. Unfortunately, the slice filter removes the latter part of the list at the given position, leaving the front half of the list so that’s not going to work. However, the lookup filter does work if you use the index. For future reference, the syntax looks like this:

{{ event.URL | split: '/' | lookup: 4 }}

Your response does lead me to another question, though. Regarding the Python custom action you say, “you’ll need to process the URL in Python and pass CUSTOMERID and EVENTID as separate properties in the Klaviyo event payload.” I am able to process the URL in Python, but how do I pass the variables back as separate properties in the Klaviyo event payload?

I’ve tried setting event["data"]["attributes"]["event_properties"]['CUSTOMERID'] = CUSTOMERID and returning event. This does not seem to work.


MANSIR2094
Problem Solver IV
Forum|alt.badge.img+17
  • Problem Solver IV
  • 241 replies
  • Answer
  • February 21, 2025

  ​@robdentonrachio   I appreciate your insights on the lookup filter—you're absolutely right, and that’s a great approach. Thanks for sharing that!

Regarding your question about passing variables in the Klaviyo event payload, I recently completed a similar project for another client and can definitely help you with this. The key is ensuring that the event properties are structured correctly before sending the payload.

Here’s the recommended structure:

event_payload = {
    "data": {
        "type": "event",
        "attributes": {
            "event_name": "Your Event Name",
            "event_properties": {
                "CUSTOMERID": CUSTOMERID,
                "EVENTID": EVENTID
            }
        }
    }
}

Make sure the request is properly formatted when sending the event to Klaviyo. If you’re using a POST request to Klaviyo’s API, ensure that the content type is set to application/json and that the payload is correctly encoded.

If you're still running into issues, I’d be happy to take a closer look and assist further. Let me know how you'd like to proceed!


Forum|alt.badge.img+1

Ah, I see. So a new event payload is required, you can’t modify the existing event payload in the current flow. Ok, thanks for all your help!


MANSIR2094
Problem Solver IV
Forum|alt.badge.img+17
  • Problem Solver IV
  • 241 replies
  • February 24, 2025

@robdentonrachio 

Yes, exactly! A new event payload is required since Klaviyo doesn’t allow modifying past events. Glad I could help! Let me know if you need any further clarification or assistance with the implementation.