几个月前,我们将 AMP 引入了我们的 Rails 应用程序。我们的实现包括以下内容:
<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 中设置自定义变量而不对变量名称有任何限制?如果是这样,怎么办?
最佳答案
您应该注意到Custom Variables仅适用于旧版谷歌分析跟踪。对于最新的实现,您需要将自定义变量替换为自定义维度。您可以查看迁移指南Here和 Here .
完成迁移后,您可以查看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>
关于ruby-on-rails - 如何在 AMP 分析中包含自定义变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47861678/