From e51f78f2068155691eba9cf4cdcb90a938fd1664 Mon Sep 17 00:00:00 2001 From: Cole Goldsmith Date: Thu, 19 Dec 2024 13:01:00 -0600 Subject: [PATCH 1/2] add track call to copy button --- src/js/07-copy-to-clipboard.js | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/js/07-copy-to-clipboard.js b/src/js/07-copy-to-clipboard.js index d64df7ab..49ab7a97 100644 --- a/src/js/07-copy-to-clipboard.js +++ b/src/js/07-copy-to-clipboard.js @@ -49,7 +49,13 @@ toolbox.appendChild(copy) } if (copy) { - copy.addEventListener('click', writeToClipboard.bind(copy, code)) + copy.addEventListener('click', function () { + var text = code.innerText.replace(TRAILING_SPACE_RX, '') + if (code.dataset.lang === 'console' && text.startsWith('$ ')) text = extractCommands(text) + var codeSample = text.slice(0, 50).trim() + writeToClipboard(text, copy) + trackCopy(code.dataset.lang, title?.childNodes[0]?.nodeValue, codeSample) + }) content.prepend(toolbox) } if (lang && !title && !nolang) { @@ -69,9 +75,7 @@ return cmds.join(' && ') } - function writeToClipboard (code) { - var text = code.innerText.replace(TRAILING_SPACE_RX, '') - if (code.dataset.lang === 'console' && text.startsWith('$ ')) text = extractCommands(text) + function writeToClipboard (text, button) { window.navigator.clipboard.writeText(text).then( function () { const icon = this.querySelector('.material-icons') @@ -83,8 +87,18 @@ setTimeout(function () { icon.innerText = 'content_paste' }, 500) - }.bind(this), + }.bind(button), function () {} ) } + + function trackCopy (language, title, sample) { + if (window.analytics) { + window.analytics.track('Code Snippet Copied', { + snippetLanguage: language, + snippetTitle: title, + snippetSample: sample, + }) + } + } })() From 192212d1b24e282aa8df79e08dd94fe035fc42cf Mon Sep 17 00:00:00 2001 From: Cole Goldsmith Date: Thu, 19 Dec 2024 13:29:23 -0600 Subject: [PATCH 2/2] clean up sample code sent to segment --- src/js/07-copy-to-clipboard.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/js/07-copy-to-clipboard.js b/src/js/07-copy-to-clipboard.js index 49ab7a97..559e9d29 100644 --- a/src/js/07-copy-to-clipboard.js +++ b/src/js/07-copy-to-clipboard.js @@ -52,9 +52,8 @@ copy.addEventListener('click', function () { var text = code.innerText.replace(TRAILING_SPACE_RX, '') if (code.dataset.lang === 'console' && text.startsWith('$ ')) text = extractCommands(text) - var codeSample = text.slice(0, 50).trim() writeToClipboard(text, copy) - trackCopy(code.dataset.lang, title?.childNodes[0]?.nodeValue, codeSample) + trackCopy(code.dataset.lang, title?.childNodes[0]?.nodeValue, text) }) content.prepend(toolbox) } @@ -92,8 +91,9 @@ ) } - function trackCopy (language, title, sample) { + function trackCopy (language, title, text) { if (window.analytics) { + var sample = text.slice(0, 50).replace(/\s+/g, ' ').trim() window.analytics.track('Code Snippet Copied', { snippetLanguage: language, snippetTitle: title,