Javascript 谷歌标签管理器 (GTM) 不适用于 SharePoint
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26134131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google Tag Manager (GTM) not working on SharePoint
提问by Janith Widarshana
I added the GTM code inside the SharePoint Master Page inside the body tag.
我在正文标记内的 SharePoint 母版页中添加了 GTM 代码。
<body>
<--Body content goes here -->
<!-- Google Tag Manager -->
<noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-XXXXXXXX"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<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=
'//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-T9XLSX');</script>
<!-- End Google Tag Manager -->
</body>
But It seems that the script is not properly fired as the whole content is wrapped by a form element as following.It generate by SharePoint.
但似乎脚本没有正确触发,因为整个内容都被一个表单元素包裹,如下所示。它由 SharePoint 生成。
<body>
<form method="post" action="" onsubmit="javascript:return WebForm_OnSubmit();" id="aspnetForm">
<--Body content goes here -->
<!-- Google Tag Manager -->
<noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-XXXXXXXX"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<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=
'//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-T9XLSX');</script>
<!-- End Google Tag Manager -->
</form>
</body>
Is there a way we can properly place the tag in SharePoint?
有没有办法可以在 SharePoint 中正确放置标签?
采纳答案by Indika Sugathsiri
There is one workaround. You can directly editthe '.master' page in SharePoint 2013 usingsharepoint designerand insert the the GTMjust after the closing tagof sharepoint form as below,
有一种解决方法。您可以直接编辑了“的.master在SharePoint 2013”页面使用SharePoint Designer中,插入的GTM刚刚结束标记后的SharePoint形式如下,
</SharePoint:SharePointForm>
<!-- Google Tag Manager -->
<noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-XXXXXXXX"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<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=
'//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','sss');</script>
<!-- End Google Tag Manager -->
回答by Henry Ong
I was able to solve it for a SharePoint Online site collection. Assuming that the Publishing Features are turned on, I:
我能够为 SharePoint Online 网站集解决此问题。假设发布功能已打开,我:
- Put all the Google Tag Manager stuff into a dedicated HTML file.
- Upload the HTML file somewhere like the root Site Assets library.
- Go to the Design Manager > Snippet Tool
- Configure a Content Editor Web Part to render the HTML file.
- Embed the Content Editor Web Part into your master page with no chrome/make invisible with your preferred method of choice or use the native ms-hideclass
- Check-in, publish.
- 将所有 Google Tag Manager 内容放入一个专用的 HTML 文件中。
- 将 HTML 文件上传到某个位置,例如根站点资产库。
- 转到设计管理器 > 代码段工具
- 配置内容编辑器 Web 部件以呈现 HTML 文件。
- 将内容编辑器 Web 部件嵌入到您的母版页中,没有镶边/使用您选择的首选方法或使用本机ms-hide类使不可见
- 签到,发布。
回答by Indika Sugathsiri
You can find a sandbox solution for this in codeplex. I guarantee this works fine. all installation guidance is there.
您可以在 codeplex 中找到一个沙盒解决方案。我保证这工作正常。所有安装指南都在那里。

