In my quest to extract the dynamic "sku" value from GTM, I am faced with a challenge. This particular value is unique for each product page.
Below is an example of how the code appears on the page:
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "Product",
"sku": "305FS-UJNA5",
"offers": [{
"@type": "Offer",
"price": "99.00",
"availability": "InStock",
"priceCurrency": "USD"
}]
}
</script>
The goal is to capture the variable: 305FS-UJNA5 (without quotes).
One approach I've considered is using a custom JavaScript variable in GTM that can search for "sku": "
within the page and retrieve the value up to the next double quote.
If this method proves difficult, what alternative would you suggest?
My attempts at searching have so far yielded undefined results within GTM...
function () {
var content = document.body.innerText;
var query=""sku": "";
if (content.search(query) > -1 ) {
return true;
} else {
return false;
}
}