我正在尝试将内容分组变量分配到我的AMP标记中的Google Analytics代码。
我的AMP分析代码:
<amp-analytics type="googleanalytics" id="analyticsgoogle1">
<script type="application/json">
{
"vars": {
"account": "UA-XXXXXXXX-X"
},
"triggers": {
"trackPageview": {
"on": "visible",
"request": "pageview"
}
}
}
</script>
</amp-analytics>
我的常规Google分析代码:
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXXXXX-X', 'auto');
ga('set', 'contentGroup1', 'My Category');
ga('set', 'contentGroup2', 'My Author Type');
ga('set', 'contentGroup3', 'My Author Name');
ga('send', 'pageview');
</script>
我知道它没有在文档中列出,但我正在寻找可能有用的东西,以便我的AMP页面可以跟常规页面一样被跟踪。
可能的AMP项目包含内容分组的Google Analytics代码:
<amp-analytics type="googleanalytics" id="analyticsgoogle1">
<script type="application/json">
{
"vars": {
"account": "UA-XXXXXXXX-X"
},
"triggers": {
"trackPageview": {
"on": "visible",
"request": "pageview"
"vars": {
"contentGroup1": "My Category",
"contentGroup2": "My Author Type",
"contentGroup3": "My Author Name"
}
}
}
}
</script></amp-analytics>
我没有看到内容分组变量何时查看amp-analytics支持的变量文档: https://github.com/ampproject/amphtml/blob/master/extensions/amp-analytics/analytics-vars.md
以下是有关amp-analytics代码的文档:https://developers.google.com/analytics/devguides/collection/amp-analytics/
答案 0 :(得分:2)
我做了一个有效的解决方案。实际上它很简单。以下代码的相同注释。
的详细信息cg1 = contentGroup1,cg2 = contentGroup2
重要提示:如果您想查看对GA的真实请求,请在浏览器中使用 限制 。在其他情况下,您将看到来自GA的重定向链而不是您的真实请求。很烦人。
<amp-analytics type="googleanalytics" id="ga1">
<script type="application/json">
{
"extraUrlParams" : {
"cd4": "AMP"
<?php if($this->tracking_group_config['group'] === 'contentGroup1'):?>
,"cg1": "<?=$this->tracking_group_config['type']?>"
<?php elseif($this->tracking_group_config['group'] === 'contentGroup2'): ?>
,"cg2": "<?=$this->tracking_group_config['type']?>"
<?php endif; ?>
},
"vars": {
"account": "[Your GA account ID here]"
},
"triggers": {
"pageviewCustom": {
"on": "visible",
"request": "pageview"
}
}
}
</script>
</amp-analytics>
答案 1 :(得分:0)
使用gtag,此代码将起作用:
<amp-analytics type="gtag" data-credentials="include">
<script type="application/json">
{
"vars": {
"gtag_id": "UA-XXXXXXXX-YY",
"config": {
"UA-XXXXXXXX-YY": {
"groups": "default",
"content_group1": "my value",
"content_group3": "my other value"
}
}
}
}
</script>
</amp-analytics>
您还可以使用“网络”标签下的Chrome开发工具来调试它:
希望有帮助!