Hi Team,
I would like to add the new field in a Metric Value Property dropdown. so that we can use that property as filter for segmentation within flows and metrics.
For ex. We have existing API metrics “Add to cart” and which has pre-built properties such as basketgross, itemCount etc. so i want to create a new custom metric “Add To Cart - Custom” and add this new propety to that metric definition.
Can you please help me to achieve this ?
Thanks
Prashant
Need to add new field as a filter to the custom metric
Best answer by Christiannoerbjerg
Hi
Thank you for posting in the Community!
Here’s how you can it up using Google Tag manager.
Step 1: Create a new tag in GTM:
- Log in to Google Tag Manager.
- Navigate to Tags > New.
- Name the tag (e.g.,
Klaviyo Add to Cart - Custom
).
Step 2: Configure the tag:
-
Choose tag type:
- Select Custom HTML tag.
-
Insert the Klaviyo Track API code: Add the following code to the HTML field:
<script> (function() { var dataLayerEvent = {{Event}}; var basketgross = {{BasketGross}}; var itemCount = {{ItemCount}}; var customProperty = {{CustomProperty}}; var email = {{UserEmail}}; // Ensure email is captured in your data layer // Replace YOUR_PUBLIC_API_KEY with your Klaviyo API key var payload = { "token": "YOUR_PUBLIC_API_KEY", "event": "Add to Cart - Custom", "customer_properties": { "$email": email }, "properties": { "basketgross": basketgross, "itemCount": itemCount, "custom_property": customProperty } }; var xhr = new XMLHttpRequest(); xhr.open("POST", "https://a.klaviyo.com/api/track", true); xhr.setRequestHeader("Content-Type", "application/json"); xhr.send(JSON.stringify(payload)); })(); </script>
-
Customize variables: Replace
{{Event}}
,{{BasketGross}}
,{{ItemCount}}
,{{CustomProperty}}
, and{{UserEmail}}
with the appropriate GTM variables.
If the "application/json"
doesnt work. You can try with the following: "application/vnd.api+json"
Step 3: Create variables in GTM:
-
Go to variables in GTM.
-
Create the following variables based on your data layer:
- Event: Captures the type of event (e.g., Add to Cart).
- BasketGross: Captures the total basket value.
- ItemCount: Captures the number of items in the cart.
- CustomProperty: Captures your custom metric data.
- UserEmail: Captures the user’s email address (if available).
If these variables are not in your data layer, you may need to work with your developer to ensure the required data is available.
Step 4: Set up triggers:
-
Choose a trigger type:
- Use the Event trigger to fire the tag when a user adds a product to the cart.
- Example: Create a trigger that listens for an
Add to Cart
event in the data layer (e.g.,event = "add_to_cart"
).
-
Test your trigger:
- Enable GTM’s Preview mode.
- Perform an
Add to Cart
action on your website and verify that the trigger fires.
Step 5: Test and debug:
- Enable GTM’s Preview mode and verify:
- The tag fires when a product is added to the cart.
- The payload includes all the necessary properties (e.g.,
basketgross
,itemCount
,custom_property
,email
).
- Check Klaviyo’s Analytics > Metrics to confirm that the new custom event (
Add to Cart - Custom
) is recorded.
Step 6: Publish the tag:
- Once testing is complete and successful, publish the changes in GTM.
Hope that helps or else, let me know! :-)
Christian Nørbjerg Enger
Partner & CPO
Web: Segmento.dk
LinkedIn: @christianfromsegmento
Voldbjergvej 22b, 8240 Risskov
Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.