简体   繁体   English

Google跟踪代码管理器:使用GTM商家代码中html页上定义的Javascript变量?

[英]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跟踪代码管理器中:

  1. Add 4 Variables with type "dataLayer" 添加4个类型为“ dataLayer”的变量
  2. set variable names to order_id, email etc. 将变量名称设置为order_id,电子邮件等。
  3. use variable in your Tags as {{order_id}} etc. 在标签中使用变量{{order_id}}等。

暂无
暂无

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

相关问题 Google跟踪代码管理器(GTM)和Google Analytics变量 - Google Tag Manager (GTM) and Google Analytics variables Google代码管理器:是否可以通过GTM将CUSTOM HTML代码放入head标签? - Google tag manager: Is it possible to put CUSTOM HTML code in head tag via GTM? 与同一页面上的两个 Google 跟踪代码管理器 (GTM) 帐户冲突 - Conflict with two Google Tag Manager (GTM) accounts on same page Javascript 在 chrome 控制台中工作,但在 Google 跟踪代码管理器 (GTM) 上不工作 - Javascript is working in the chrome console but its not working on Google Tag Manager (GTM) 将子字符串提取到 GTM 变量中的简单代码(Google Tag Manager) - Simple code to extract substring into a variable of GTM (Google Tag Manager) 如何使用Google跟踪代码管理器(GTM)跟踪加密的电子邮件链接 - How to track encrypted email links using Google Tag Manager (GTM) 通过使用纯Javascript在单页应用程序上使用Google跟踪代码管理器 - Using Google Tag Manager on a Single Page Application by using plain Javascript 将 Google 跟踪代码管理器 (gtm) 安装到 Angular 应用程序 - Install Google Tag Manager (gtm) to Angular app 自定义 Google 跟踪代码管理器 gtm.elementClasses - Custom Google Tag Manager gtm.elementClasses 是否可以使用Google跟踪代码管理器覆盖我页面上的现有JavaScript? - Is it possible to overwrite existing javascript on my page using Google Tag Manager?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM