Solved

Base64 Encoding a stringified JSON object

  • 23 June 2022
  • 4 replies
  • 1147 views

Userlevel 4
Badge +7

Hi all,

I’m looking to include a number of profile properties into a b64 encoded query param for URLs to use in an email/SMS.

What I am looking for is a URL that comes out like…

`https://klaviyo.com?data=d3d3LmtsYXZpeW8uY29t`

And this `data` property, when b64 decoded, is a JSON stringified object, such as…

'{"phone": "555-555-5555", "email": "testing@example.com"}'

I want to do this so my webapp can decode the single query param and then run `JSON.parse()` on it to get all of the variables within.

Has anyone tried something similar? I’m running into a number of issues with creating stringified JSON objects with profile properties and b64 encoding them.

I’m also running into issues with creating profile properties that include variables in them...

Does anyone have any suggestions?

 

Thank you!

Kevin.

icon

Best answer by JeffV.klaviyo 28 June 2022, 01:56

View original

4 replies

Userlevel 7
Badge +60

Hello @KeviSunshine,

Welcome to the Klaviyo Community!

Seems like you’ve been in contact with our awesome Support colleagues and getting down to the nitty gritty details of your question! Would love if you could follow up with the solution that’s been discussed to further help other Community members who may be running into the same question in the future!

Thanks for being a part of our Klaviyo Community!

David

Userlevel 4
Badge +7

Hi @David To,

Sure thing!

For everyone who’s interested, we haven’t gotten to the crux of the issue yet but I did learn something very cool from one of Klaviyo’s awesome support specialists, Jeff.

If you want to set a profile property that consists of another profile property, you can use the Call Webhook step and call Klaviyo’s own Identify API. How cool is that! Here is how Jeff explained it…

Setting a property on a profile that consists of other properties
You are correct that our "Update Profile Property" Flow action doesn't take in variables. Instead you should use our Webhook Actions so make an API Request to the our Identify API to update a profile property (https://developers.klaviyo.com/en/reference/track-post)
 
Here are some articles that discuss webhook flow actions:

 
It would look like this:
?name=image.png
 
HEADERS:
Accept: text/html
Content-Type: application/x-www-form-urlencoded
 
JSON payload:

{
"token": "YOUR-PUBLIC-KEY",
"properties": {
"$email": "{{ person.email }}",
"my-custom-prop": "{{ person.email }}"
}
}

 
So the final result would be:
?name=image.png
 
$email is the profile that you want to assign the property to, then you can add any custom properties below that. You can refer to the API documentation here to learn about Identify API formatting: https://developers.klaviyo.com/en/reference/identify-post 

Userlevel 7
Badge +60

That’s a start! Gotta shout @JeffV.klaviyo for being so thorough in this!

Can’t wait to hear the full resolution!

David

Userlevel 1
Badge +4

To close the loop here, we resolved part 1 of @KeviSunshine’s original question with the flow webhook action. The next step was to give Kevin the ability to base64 encode the following text:

"phone: <customer_phone_number>"

This was a bit tricky as we needed to take a string (“phone: “) and combine it with a dynamic variable (customer_phone_number), and then 64 encode the entire thing. Since our email templates use Django syntax, we needed to do the following:

{% with phoneNum=person.phone_number 
text='{"phone": ' %}
{{ text|add:'"'|add:phoneNum|add:'"}'|base64_encode }}
{% endwith %}

Essentially, we need to save the string “phone: “ as one variable and then the customer’s phone number ( {{person.phone_number}} ) as another variable. We then used the django |add filter to concatenate/combine the two strings, and the |base64_encode django filter to encode everything.

Thank you for your patience @KeviSunshine and let us know if there is anything unexpected occurring with this solution. 

Reply