Error: Invalid JSON: unexpected token

citrusc3
Visitor
1 0 0

I am trying to add code in my Dawn theme for web tracking.

 

Klaviyo says to enter this code into the product.liquid template.

 

<script type="text/javascript">
var _learnq = _learnq || [];

var item = {
Name: {{ product.title|json }},
ProductID: {{ product.id|json }},
Categories: {{ product.collections|map:'title'|json }},
ImageURL: "https:{{ product.featured_image.src|img_url:'grande' }}",
URL: "{{ shop.secure_url }}{{ product.url }}",
Brand: {{ product.vendor|json }},
Price: {{ product.price|money|json }},
CompareAtPrice: {{ product.compare_at_price_max|money|json }}
};

_learnq.push(['track', 'Viewed Product', item]);
_learnq.push(['trackViewedItem', {
Title: item.Name,
ItemId: item.ProductID,
Categories: item.Categories,
ImageUrl: item.ImageURL,
Url: item.URL,
Metadata: {
Brand: item.Brand,
Price: item.Price,
CompareAtPrice: item.CompareAtPrice
}
}]);
</script>

 

 

I have been trying to add this into the product.json (as I think that is the equivalent of product.liquid), but then I get the error of "Error: Invalid JSON: unexpected token".

 

Any help would be greatly appreciated.

Replies 3 (3)
Laura-Hirtop
Shopify Partner
22 5 6

You cannot add it in the product.json file, as it only accepts a schema written in json format. It should be added in a file ending in .liquid in order for it to be executed. I you do not have a specific product.liquid file, in the product.json template, you can find the main section that appears on your page in that template: bellow "sections", the first type that appears is most likely to be the central section in your product page. For instance, here it is called "product-form.liquid":

LauraHirtop_0-1647839503709.png

Find the file with that name ending in .liquid and paste the code bellow all the code, but above any {% schema %} tag, and it should be enough.

Berrywrites
Visitor
1 0 0

Thanks a lot, your answer is really helpful to me.😁

infomyhyggehome
Visitor
3 0 0

Hello, I try to add this html code and get this response:

infomyhyggehome_0-1699190217603.png

What i'm dpoing wrong ?

Thank you