简体   繁体   English

如何在 Google 标签管理器标签中使用 Shopify 液体变量

[英]How to use Shopify liquid variables in Google tag managers tag

I am fairly new to GTM.我对 GTM 相当陌生。 I need to put this script to our Shopify product pages.我需要将此脚本放入我们的 Shopify 产品页面。 It is for an email marketing automation saas.它用于电子邮件营销自动化 saas。

If I put it directly into the code of the page in Shopify, it renders well and comes back with a product id that is fished from the liquid code above.如果我将它直接放入 Shopify 中页面的代码中,它会很好地呈现并返回一个从上面的液体代码中提取的产品 ID。 It looks like this:它看起来像这样:

<script type="text/javascript">
Targito.push( 'event', 'product_view', { 'id' : '{{ product.selected_or_first_available_variant.id }}' } );
</script>

But when I want to put it into Google Tag Manager, as custom HTML tag, and then add the variable into variables in GTM, after I publish it, on the page, in inspect tool and elements, it comes back as this:但是当我想将它作为自定义 HTML 标记放入 Google 跟踪代码管理器中,然后将变量添加到 GTM 中的变量中时,在我发布它之后,在页面上,在检查工具和元素中,它返回如下:

<script type="text/javascript" id="">Targito.push("event","product_view",{id:"{{undefined}}"});</script>

I need to see a product id of the page you are currently viewing.我需要查看您当前正在查看的页面的产品 ID。

The thing is, Shopify liquid variables are also defined with double curly brackets, so are GTM variables.问题是,Shopify 液体变量也用双大括号定义,GTM 变量也是如此。 Would anyone be able to help me please?有人可以帮助我吗? Highly appreciate your tips in advance.提前非常感谢您的提示。

You can't put liquid code inside your tag.您不能将流动代码放入标签中。 The tag is rendered in Javascript that is on client side.该标记以客户端的 Javascript 呈现。 Liquid is rendered on server side. Liquid 在服务器端渲染。 You will need to populate a variable with liquid and use that in your tag.您将需要用液体填充变量并在您的标签中使用它。 There are other ways but the fastest is doing something this.还有其他方法,但最快的是这样做。

You put this in your .liquid template (somewhere at the beginning of the page)你把它放在你的 .liquid 模板中(在页面开头的某个地方)

<script>
window.myVariantId = '{{ product.selected_or_first_available_variant.id }}';
</script>

And then in your tag you can put然后在你的标签中你可以放

<script type="text/javascript">
Targito.push( 'event', 'product_view', { 'id' : myVariantId } );
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM