javascript - 将变量从 PHP 转换为 Javascript 以用于 Google Analytics 电子商务跟踪代码

标签 javascript php e-commerce google-analytics-api google-analytics-firebase

我正在尝试在网站上安装 Google Analytics 电子商务跟踪,虽然我已将其安装在正确的位置,但我不确定如何使用我的 PHP 代码动态创建电子商务交易。 Google 提供的示例 js 代码如下所示:

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXXXX-X']);
  _gaq.push(['_trackPageview']);
  _gaq.push(['_addTrans',
    '1234',           // transaction ID - required
    'Acme Clothing',  // affiliation or store name
    '11.99',          // total - required
    '1.29',           // tax
    '5',              // shipping
    'San Jose',       // city
    'California',     // state or province
    'USA'             // country
  ]);

   // add item might be called for every item in the shopping cart
   // where your ecommerce engine loops through each item in the cart and
   // prints out _addItem for each
  _gaq.push(['_addItem',
    '1234',           // transaction ID - required
    'DD44',           // SKU/code - required
    'T-Shirt',        // product name
    'Green Medium',   // category or variation
    '11.99',          // unit price - required
    '1'               // quantity - required
  ]);
  _gaq.push(['_trackTrans']); //submits transaction to the Analytics servers

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

假设我的交易 ID 是一个名为 $ID 的 PHP 变量,total 是一个名为 $amount 的变量,等等

如何在跟踪代码中使用这些变量向 Google 动态发送正确的信息?

最佳答案

如果 Javascript 在您的 HTML 输出中,您可以将 PHP echo 放在右侧内联(假设您使用的是 .php 文件而不是 .html 文件:

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXXXX-X']);
  _gaq.push(['_trackPageview']);
  _gaq.push(['_addTrans',
    <?php echo $ID; ?>, // transaction ID - required
    'Acme Clothing',    // affiliation or store name
    '11.99',            // total - required
    '1.29',             // tax
    '5',                // shipping
    'San Jose',         // city
    'California',       // state or province
    'USA'               // country
  ]);

   // add item might be called for every item in the shopping cart
   // where your ecommerce engine loops through each item in the cart and
   // prints out _addItem for each
  _gaq.push(['_addItem',
    <?php echo $ID; ?>, // transaction ID - required
    'DD44',             // SKU/code - required
    'T-Shirt',          // product name
    'Green Medium',     // category or variation
    '11.99',            // unit price - required
    '1'                 // quantity - required
  ]);
  _gaq.push(['_trackTrans']); //submits transaction to the Analytics servers

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

或者,如果您的 javascript 位于脚本文件中,您可以在加载 javascript 文件之前将 javascript 变量写入 html 页面,然后使用这个新变量(在下面的示例中 transactionID):

<script>
    var transactionID = <?php echo $ID; ?>;
</script>

<script src="js/myscript.js"></script>

关于javascript - 将变量从 PHP 转换为 Javascript 以用于 Google Analytics 电子商务跟踪代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29660216/

相关文章:

javascript - 在与网站相同的选项卡中以编程方式打开 Chrome 扩展页面

php - 可捕获的 fatal error : Object of class player could not be converted to string

php - 将 SQL 语句转换为 Joomla 格式时遇到问题

php - IE 中 HTTPS Ajax 请求的问题(非跨域)

c# - 处理许多商家网关的信用卡处理库?

javascript - 防止实时表上传的双重事件功能

带有相对变量的 JavaScript 回调函数

javascript - 当用户输入数字时,显示一个对话框

e-commerce - 如何解决我网站使用的第三方系统未告知支付成功的问题?

paypal - 购买电子书后需要 eJunkie 返回我的网站