Microsoft 澄清不适用于 Next js/React Js 项目
Microsoft clarity not working with Next js/React Js project
我正在尝试让 Next JS 项目更加清晰。
我关注了一些在线资源,这些资源显示可以通过以下方式添加 Google 标签 manager/Clarity 的脚本。
文件:app.tsx
<Script
id="clarity"
strategy="afterInteractive"
dangerouslySetInnerHTML={{
__html: `
(function(c,l,a,r,i,t,y){
c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)};
t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i;
y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y);
})(window, document, "clarity", "script", '${CLARITY_KEY}');`,
}}
/>
此代码成功触发并正在获取另一个脚本文件,该文件在执行时抛出错误。下面是 Clarity 调用并抛出错误的脚本:
!function(c, l, a, r, i, t, y) {
if (a[c].v || a[c].t)
return a[c]("event", c, "dup." + i.projectId);
a[c].t = !0,
(t = l.createElement(r)).async = !0,
t.src = "https://www.clarity.ms/eus2-f/s/0.6.34/clarity.js",
(y = l.getElementsByTagName(r)[0]).parentNode.insertBefore(t, y),
a[c]("start", i),
a[c].q.unshift(a[c].q.pop())
}("clarity", document, window, "script", {
"projectId": "XmaskedX",
"upload": "https://www.clarity.ms/eus2-f/collect",
"expire": 365,
"cookies": ["_uetmsclkid", "_uetvid"],
"track": true,
"lean": false,
"content": true,
"extract": [0, 501, "window.navigator.hardwareConcurrency", 0, 502, "window.navigator.deviceMemory", 0, 503, "window.navigator.platform", 0, 504, "window.navigator.maxTouchPoints", 0, 505, "window.devicePixelRatio", 0, 510, "screen.isExtended", 0, 511, "navigator.cookieEnabled", 0, 512, "navigator.onLine", 0, 513, "navigator.doNotTrack", 0, 514, "navigator.connection", 0, 515, "navigator.vendor", 0, 516, "navigator.product", 0, 517, "navigator.productSub", 0, 518, "navigator.pdfViewerEnabled"],
"fraud": [[1, "input[type=\u0027email\u0027]"]],
"report": "https://www.clarity.ms/report/eus2f"
});
抛出的错误如下:
line 8 Uncaught TypeError: a[c] is not a function
a[c]("start", i) // throws error on this line
已调试a为window,c为字符串"clarity"。
该代码在 React 项目中运行良好,但在 Next JS 中无法运行。
我正在使用原生脚本标签
<script type="text/javascript" >
{`(function (c, l, a, r, i, t, y) {
c[a] = c[a] || function () { (c[a].q = c[a].q || []).push(arguments) };
t = l.createElement(r); t.async = 1; t.src = "https://www.clarity.ms/tag/" + i;
y = l.getElementsByTagName(r)[0]; y.parentNode.insertBefore(t, y);})(window, document, "clarity", "script", "clarity-key");`}
</script>
问题是将 id="clarity" 分配给脚本标签。
似乎 clarity 试图插入一个 id="clarity" 的元素,这可能是问题所在。将 id 更改为其他内容有效。
我正在尝试让 Next JS 项目更加清晰。 我关注了一些在线资源,这些资源显示可以通过以下方式添加 Google 标签 manager/Clarity 的脚本。
文件:app.tsx
<Script
id="clarity"
strategy="afterInteractive"
dangerouslySetInnerHTML={{
__html: `
(function(c,l,a,r,i,t,y){
c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)};
t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i;
y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y);
})(window, document, "clarity", "script", '${CLARITY_KEY}');`,
}}
/>
此代码成功触发并正在获取另一个脚本文件,该文件在执行时抛出错误。下面是 Clarity 调用并抛出错误的脚本:
!function(c, l, a, r, i, t, y) {
if (a[c].v || a[c].t)
return a[c]("event", c, "dup." + i.projectId);
a[c].t = !0,
(t = l.createElement(r)).async = !0,
t.src = "https://www.clarity.ms/eus2-f/s/0.6.34/clarity.js",
(y = l.getElementsByTagName(r)[0]).parentNode.insertBefore(t, y),
a[c]("start", i),
a[c].q.unshift(a[c].q.pop())
}("clarity", document, window, "script", {
"projectId": "XmaskedX",
"upload": "https://www.clarity.ms/eus2-f/collect",
"expire": 365,
"cookies": ["_uetmsclkid", "_uetvid"],
"track": true,
"lean": false,
"content": true,
"extract": [0, 501, "window.navigator.hardwareConcurrency", 0, 502, "window.navigator.deviceMemory", 0, 503, "window.navigator.platform", 0, 504, "window.navigator.maxTouchPoints", 0, 505, "window.devicePixelRatio", 0, 510, "screen.isExtended", 0, 511, "navigator.cookieEnabled", 0, 512, "navigator.onLine", 0, 513, "navigator.doNotTrack", 0, 514, "navigator.connection", 0, 515, "navigator.vendor", 0, 516, "navigator.product", 0, 517, "navigator.productSub", 0, 518, "navigator.pdfViewerEnabled"],
"fraud": [[1, "input[type=\u0027email\u0027]"]],
"report": "https://www.clarity.ms/report/eus2f"
});
抛出的错误如下:
line 8 Uncaught TypeError: a[c] is not a function a[c]("start", i) // throws error on this line
已调试a为window,c为字符串"clarity"。
该代码在 React 项目中运行良好,但在 Next JS 中无法运行。
我正在使用原生脚本标签
<script type="text/javascript" >
{`(function (c, l, a, r, i, t, y) {
c[a] = c[a] || function () { (c[a].q = c[a].q || []).push(arguments) };
t = l.createElement(r); t.async = 1; t.src = "https://www.clarity.ms/tag/" + i;
y = l.getElementsByTagName(r)[0]; y.parentNode.insertBefore(t, y);})(window, document, "clarity", "script", "clarity-key");`}
</script>
问题是将 id="clarity" 分配给脚本标签。
似乎 clarity 试图插入一个 id="clarity" 的元素,这可能是问题所在。将 id 更改为其他内容有效。