如何通过服务器上的Google跟踪代码管理器设置Analytics(分析)变量? [英] How can I set Analytics variables with Google Tag Manager from the server?

查看:119
本文介绍了如何通过服务器上的Google跟踪代码管理器设置Analytics(分析)变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google Optimize创建A/B测试.我按照本指南的说明在服务器端模式下使用它: https://developers.google. com/optimize/devguides/experiments

I'm using Google Optimize for creating A/B tests. I'm using it in server side mode as in this guide: https://developers.google.com/optimize/devguides/experiments

该指南显示了一种简单的方法,可以通过在服务器上渲染设置实验ID和变体ID的JS代码来设置使用哪个变体运行哪个实验:

That guide shows an easy way of setting which experiment is running with which variant by rendering the JS code on the server which sets the experiment id and variant id:

  // 2. Create a tracker.
  ga('create', 'UA-XXXXX-Y', 'auto');

<?php
<<<HTML
  // 3. Set the experiment ID and variation ID.
  ga('set', 'exp', '$experimentId.$variationId');
HTML;
?>
  // 4. Send a pageview hit to Google Analytics.
  ga('send', 'pageview');

但是,我使用的是Google跟踪代码管理器,到目前为止,还没有找到任何指南来显示如何使用它来设置服务器中的变量. ga是未定义的变量,因此以上操作无效.

However I'm using Google Tag manager and so far haven't managed to find any guide that shows how to set variables from the server with it. ga is an undefined variable so the above doesn't work.

推荐答案

由于GTM调用了每个代码的常规代码段,因此您可以设置分析代码段可以理解的任何字段,即使这些代码段尚未自动列在代码标签编辑器中下来.

Since the GTM calls the normal snippet for each tag, you can set any field the analytics snippet understands even if they are not yet automatically listed in the tag editor drop down.

例如,作为页面查看字段:

For example, as a page view field:

然后设置DataLayer变量,以便可以从外部源接收它,例如:

Then set DataLayer variable so it can be received from the external source, for example:

使用完成的Tag中的变量来引导字段:

Leading to the fields using the variable in a finished Tag:

现在,您可以在服务器端设置DataLayer变量,该变量将传递给标签.由于我选择了页面视图,因此最好在加载GTM之前预先填充dataLayer,这样它们就可以在初始代码触发之前出现,例如:

Now, you can set DataLayer variable in the server side that will be passed through to the tag. Since I chose a page view, it would be best to pre-fill the dataLayer before loading the GTM so they are present before the initial tags fire, for example:

<!-- Google Tag Manager -->
<?php or other backend language wrapping...
 <script>window.dataLayer = [{exp:"$experimentId.$experimentVariant"}] 
 </script>
?>
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-0');</script>
<!-- End Google Tag Manager -->

这篇关于如何通过服务器上的Google跟踪代码管理器设置Analytics(分析)变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆