[英]Google Tag Manager: Using Javascript variables defined on page html in GTM Merchant code?
I am trying to integrate Google Merchant Code via GTM on a product checkout/thankyou page. 我正在尝试通过GTM在产品结帐/谢谢页面上集成Google Merchant Code。 The Google Merchant code is : Google商家代码为:
<script>
window.renderOptIn = function() {
window.gapi.load('surveyoptin', function() {
window.gapi.surveyoptin.render(
{
"merchant_id": 12345678,
"order_id": "ORDER_ID",
"email": "CUSTOMER_EMAIL",
"delivery_country": "COUNTRY_CODE",
"estimated_delivery_date": "YYYY-MM-DD"
});
});
}
</script>
The code need some variables (order_id, email, delivery_country, estimated_delivery_date). 该代码需要一些变量(order_id,电子邮件,delivery_country,estimate_delivery_date)。 But these variables values can only be retrieved by Magento/Php code as: var gmorderid = "loadByIncrementId($this->getOrderId())->getId(); ?>"; 但是这些变量值只能由Magento / Php代码检索为:var gmorderid =“ loadByIncrementId($ this-> getOrderId())-> getId();?>”;
var gmcemail = "<?php echo Mage::getModel('sales/order')->loadByIncrementId($this->getOrderId())->getCustomerEmail(); ?>";
var gmcountry = "<?php echo Mage::getModel('sales/order')->loadByIncrementId($this->getOrderId())->getBillingAddress()->getCountry(); ?>";
var gmdate = "<?php echo date('Y-m-d', strtotime('+5 days',Mage::getModel('core/date')->timestamp())); ?>";
</script>
I have tried putting these variables in GTM code by php echo statement as shown above. 我已尝试通过php echo语句将这些变量放入GTM代码中,如上所示。 But the php code is converted into plan text when the GTM tag is fired. 但是,在触发GTM标签后,php代码将转换为计划文本。 As a solution I defined variables in the page html andtried inserting these variables in the GTM code like: 作为解决方案,我在html页面中定义了变量,并尝试将这些变量插入GTM代码中,例如:
"order_id": gmorderid, //doesnot work. Variable shown as plan text when the code is executed
"order_id'":'+ gmorderid +'"', // also doesnot work, Variable shown as plan text when the code is executed
"order_id'":'+ {{gmorderid}} +'"', // GTM variable format, this also doesnot work
How can I use these variables values in the GTM google merchant code above so that the GTM code gets all values needed when the page is opened? 如何在上面的GTM谷歌商家代码中使用这些变量值,以使GTM代码在打开页面时获得所需的所有值?
You should save the values in global JavaScript variables or in dataLayer and then retrieve these values using GTM JavaScript variables or dataLayer variables. 您应该将这些值保存在全局JavaScript变量或dataLayer中,然后使用GTM JavaScript变量或dataLayer变量检索这些值。
For example, using your backend you create global JavaScript variable order_number and assign a value 12345 to it. 例如,使用您的后端创建全局JavaScript变量order_number并为其分配值12345。
Then in GTM you create a new variable with JavaScript variable type. 然后在GTM中,使用JavaScript变量类型创建一个新变量。 This variable should get value from order_number global JS variable. 此变量应从order_number全局JS变量获取值。 Let's say you name this GTM variable CJS - order_number. 假设您将这个GTM变量命名为CJS-order_number。
Now you create custom HTML tag and use just created GTM JS variable in it to pass required value from order_number global JS variable. 现在,您创建自定义HTML标记,并在其中使用刚刚创建的GTM JS变量,以从order_number全局JS变量传递所需的值。 You do this using double curly braces notation like {{CJS - order_number}}. 您可以使用双大括号符号(例如{{CJS-order_number}})来执行此操作。 This notation is for inner GTM variables only. 此符号仅适用于内部GTM变量。
In Magento (add js after GTM snippet) 在Magento中 (在GTM代码段后添加js)
dataLayer.push({
'order_id': '<?php echo $this->getOrderId(); ?>',
'email': '<?php echo Mage::getModel('sales/order')->loadByIncrementId($this->getOrderId())->getCustomerEmail(); ?>',
'delivery_country': '<?php echo Mage::getModel('sales/order')->loadByIncrementId($this->getOrderId())->getBillingAddress()->getCountry(); ?>',
'estimated_delivery_date': '<?php echo date('Y-m-d', strtotime('+5 days',Mage::getModel('core/date')->timestamp())); ?>'
});
In Google Tag Manager: 在Google跟踪代码管理器中:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.