Solved

After an order is placed, how can I add a custom metafield value from Shopify as an order event property in Klaviyo?


Badge +1

I've created a webhook in the Shopify admin as order creation by using following url:
  

 https://a.klaviyo.com/api/track?token=<public_api_key>

After that, I use the Klaviyo API to receive the webhook data and store it as an event in Klaviyo with the Shopify metafield value as a custom property:
 

	$url = 'https://a.klaviyo.com/api/track';
$data = array(
'token' => $public_api_key,
'event' => 'Order Placed',
'customer_properties' => array(
'$email' => '{{ order.email }}'
),
'properties' => array(
'Order ID' => '{{ order.order_number }}',
'Order Total' => '{{ order.total_price }}',
'Shopify Metafield' => '{{ order.metafields.custom.promise_date }}'
)
);

$data_string = http_build_query(array('data' => json_encode($data)));

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded',
'Content-Length: ' . strlen($data_string)
));

$result = curl_exec($ch);

curl_close($ch);

What I want is that the metafield 'promise_date' should have in the order event property list:

and then I want to show the ‘promise_date’ value in the email template.
What did I do wrong? Any help would be appreciated, thanks in advance! 
 

icon

Best answer by Brian Turcotte 4 May 2023, 21:57

View original

5 replies

Userlevel 7
Badge +36

Hi @mohammadliton91!

 

It appears you’re using our Track API, and I just wanted to clarify your use case and explain a little bit about this endpoint. Our Track API is a v2 API endpoint that creates custom metrics and sends them to Klaviyo. If you haven’t already seen it, we have a new v3 endpoint called Create Event that accomplishes the same thing, and it may be helpful to transition to that endpoint since the v1-v2 endpoints will be depreciated by the end of the year (2023). 

 

Regardless, I can see in your code that you named this custom event “Order Placed”, which you can find in the Analytics > Metrics section of your account. If you select API from the Integrations drop-down or search for the name of the event, you will see this custom “Order Placed” event. From here you will see the properties you included in the code for this custom event, such as ‘promise_date’. You can create a flow that is triggered by this custom event, and you can include the value from ‘promise_date’.

 

To clarify - what you’re creating here is an entirely new custom metric called ‘Order Placed’, which is separate from the default Shopify “Placed Order” metric. The Track API does not modify the pre-existing Shopify “Placed Order” metric. In the screenshot, it appears that you are triggering the flow with the Shopify “Placed Order” metric, which is why there’s no ‘promise_date’.

 

Separately, if you wanted to include item information in the email, you would need to include that information in your custom “Order Placed” Event.

 

I hope this helps, and thanks for using the Community!

- Brian

Badge +1

Hi @Brian Turcotte!
Thanks for your reply.
 

Badge +1

Hi @Brian Turcotte,
I have followed your instructions and it seems that this is exactly what I was looking for. I have one more question for you: Is there any documentation available for the event property list, which would assist me in creating a custom metric based on that list? Thanks again in advance!   

Userlevel 7
Badge +36

Hi @mohammadliton91!

Here is the suggested/default structure for a custom Placed Order event, which includes the properties we recommend:

 

Best,

Brian

Badge +1

Hi @Brian Turcotte 
Thanks for your kind help. I’ve already created a custom-placed order metric with the help of your instructions. Thanks again.

Reply