如何在AMP分析中包含自定义变量?

时间:2017-12-18 03:14:24

标签: ruby-on-rails google-analytics amp-html

几个月前,我们在我们的Rails应用程序中引入了AMP。我们的实施包括以下内容:

<amp-analytics type="googleanalytics">
  <script type="application/json">
  {
    "vars": {
      "account": <%= ga.profile_code.inspect.html_safe %>
    },
    "triggers": {
      "trackPageview": {
        "on": "visible",
        "request": "pageview"
      }
    }
  }
  </script>
</amp-analytics>

但是,我们现在意识到我们缺少一些重要的自定义变量,这些变量在我们的非AMP网页的Google Analytics脚本中使用。这些在脚本中设置如下(其中_gaq是一个数组):

  <% ga.variables.each do |vars| %>
    _gaq.push([ '_setCustomVar', <%= vars[:placement] %>, '<%= vars[:label] %>', '<%= vars[:variable] %>', <%= vars[:scope_number] %> ]);
  <% end %>

AMP Analytics是否可以在不对变量名称进行任何限制的情况下设置自定义变量?如果是这样,怎么样?

1 个答案:

答案 0 :(得分:2)

You should notice that Custom Variables are only available for legacy google analytics tracking. For the latest implementation, you will need to replace your custom variables with custom dimensions instead. You could check the migration guide Here and Here.

After you have made a migration, you can check the implementation of sending custom dimensions and custom metrics in AMP page.

For example, you can send a custom dimension with a pageview by including the Custom Dimension parameter (or any other parameters you want to include with the hit) in the extraUrlParams section. This section can be included at the trigger level for single requests or at a global level to send the data with all requests.

<amp-analytics type="googleanalytics">
<script type="application/json">
{
  "vars": {
    "account": "UA-XXXXX-Y"
  },
  "extraUrlParams": {
    "cd3": "AMP"
  },
  "triggers": {
    "trackPageviewWithCustomData": {
      "on": "visible",
      "request": "pageview"
    },
    "trackEvent" : {
      "on": "visible",
      "request": "event",
      "vars": {
        "eventCategory": "ui-components",
        "eventAction": "header-click"
      },
      "extraUrlParams": {
        "ni": "1"
      }
    }
  }
}
</script>
</amp-analytics>