|
49 | 49 | toolbox.appendChild(copy)
|
50 | 50 | }
|
51 | 51 | if (copy) {
|
52 |
| - copy.addEventListener('click', writeToClipboard.bind(copy, code)) |
| 52 | + copy.addEventListener('click', function () { |
| 53 | + var text = code.innerText.replace(TRAILING_SPACE_RX, '') |
| 54 | + if (code.dataset.lang === 'console' && text.startsWith('$ ')) text = extractCommands(text) |
| 55 | + writeToClipboard(text, copy) |
| 56 | + trackCopy(code.dataset.lang, title?.childNodes[0]?.nodeValue, text) |
| 57 | + }) |
53 | 58 | content.prepend(toolbox)
|
54 | 59 | }
|
55 | 60 | if (lang && !title && !nolang) {
|
|
69 | 74 | return cmds.join(' && ')
|
70 | 75 | }
|
71 | 76 |
|
72 |
| - function writeToClipboard (code) { |
73 |
| - var text = code.innerText.replace(TRAILING_SPACE_RX, '') |
74 |
| - if (code.dataset.lang === 'console' && text.startsWith('$ ')) text = extractCommands(text) |
| 77 | + function writeToClipboard (text, button) { |
75 | 78 | window.navigator.clipboard.writeText(text).then(
|
76 | 79 | function () {
|
77 | 80 | const icon = this.querySelector('.material-icons')
|
|
83 | 86 | setTimeout(function () {
|
84 | 87 | icon.innerText = 'content_paste'
|
85 | 88 | }, 500)
|
86 |
| - }.bind(this), |
| 89 | + }.bind(button), |
87 | 90 | function () {}
|
88 | 91 | )
|
89 | 92 | }
|
| 93 | + |
| 94 | + function trackCopy (language, title, text) { |
| 95 | + if (window.analytics) { |
| 96 | + var sample = text.slice(0, 50).replace(/\s+/g, ' ').trim() |
| 97 | + window.analytics.track('Code Snippet Copied', { |
| 98 | + snippetLanguage: language, |
| 99 | + snippetTitle: title, |
| 100 | + snippetSample: sample, |
| 101 | + }) |
| 102 | + } |
| 103 | + } |
90 | 104 | })()
|
0 commit comments