Skip to content

Commit 744eb8d

Browse files
authored
Merge pull request #757 from ZenUml/fix/clarity-for-iframe
feat(ContentWrap): enhance iframe handling with srcdoc and add Micros…
2 parents 187061f + 5dc0307 commit 744eb8d

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

src/components/ContentWrap.jsx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,19 @@ export default class ContentWrap extends Component {
184184
if (this.detachedWindow) {
185185
this.detachedWindow.postMessage({ contents }, '*');
186186
} else {
187-
this.frame.src = this.frame.src;
188-
setTimeout(() => {
189-
that.frame.contentDocument.open();
190-
that.frame.contentDocument.write(contents);
191-
that.frame.contentDocument.close();
192-
}, 10);
187+
// Use srcdoc attribute which works better with Microsoft Clarity
188+
// This allows Clarity to properly track interactions inside the iframe
189+
if ('srcdoc' in this.frame) {
190+
this.frame.srcdoc = contents;
191+
} else {
192+
// Fallback for older browsers that don't support srcdoc
193+
this.frame.src = 'about:blank';
194+
setTimeout(() => {
195+
that.frame.contentDocument.open();
196+
that.frame.contentDocument.write(contents);
197+
that.frame.contentDocument.close();
198+
}, 10);
199+
}
193200
}
194201
}
195202

src/utils.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,22 @@ export function getCompleteHtml(html, css, js, item) {
272272
'<meta charset="UTF-8" />\n' +
273273
'<style id="zenumlstyle">\n' +
274274
css +
275-
'\n</style>\n' +
276-
'</head>\n' +
275+
'\n</style>\n';
276+
277+
// Add Microsoft Clarity tracking code to the iframe content
278+
// This ensures Clarity can track user interactions inside the iframe
279+
if (window.clarity) {
280+
contents +=
281+
'<script type="text/javascript">\n' +
282+
'(function(c,l,a,r,i,t,y){\n' +
283+
' c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)};\n' +
284+
' t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i;\n' +
285+
' y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y);\n' +
286+
'})(window, document, "clarity", "script", window.CLARITY_PROJECT_ID || "");\n' +
287+
'</script>\n';
288+
}
289+
290+
contents += '</head>\n' +
277291
'<body>\n' +
278292
html +
279293
'\n';
@@ -299,6 +313,7 @@ export function getCompleteHtml(html, css, js, item) {
299313

300314
return contents;
301315
}
316+
/* eslint-disable max-params */
302317

303318
export function saveAsHtml(item) {
304319
var htmlPromise = computeHtml(item.html, item.htmlMode);

0 commit comments

Comments
 (0)