1
1
import $ from 'jquery' ;
2
+ import { context } from './page-context.js' ;
2
3
3
4
function initialize ( ) {
4
5
var codeBlockSelector = '.article--content pre' ;
@@ -68,9 +69,94 @@ function initialize() {
68
69
// Trigger copy failure state lifecycle
69
70
70
71
$ ( '.copy-code' ) . click ( function ( ) {
71
- let text = $ ( this )
72
+ let codeElement = $ ( this )
72
73
. closest ( '.code-controls' )
73
- . prevAll ( 'pre:has(code)' ) [ 0 ] . innerText ;
74
+ . prevAll ( 'pre:has(code)' ) [ 0 ] ;
75
+
76
+ let text = codeElement . innerText ;
77
+
78
+ // Extract additional code block information
79
+ const codeBlockInfo = extractCodeBlockInfo ( codeElement ) ;
80
+
81
+ // Add Google Analytics event tracking
82
+ const currentUrl = new URL ( window . location . href ) ;
83
+
84
+ // Determine which tracking parameter to add based on product context
85
+ switch ( context ) {
86
+ case 'cloud' :
87
+ currentUrl . searchParams . set ( 'dl' , 'cloud' ) ;
88
+ break ;
89
+ case 'core' :
90
+ /** Track using the same value used by www.influxdata.com pages */
91
+ currentUrl . searchParams . set ( 'dl' , 'oss3' ) ;
92
+ break ;
93
+ case 'enterprise' :
94
+ /** Track using the same value used by www.influxdata.com pages */
95
+ currentUrl . searchParams . set ( 'dl' , 'enterprise' ) ;
96
+ break ;
97
+ case 'serverless' :
98
+ currentUrl . searchParams . set ( 'dl' , 'serverless' ) ;
99
+ break ;
100
+ case 'dedicated' :
101
+ currentUrl . searchParams . set ( 'dl' , 'dedicated' ) ;
102
+ break ;
103
+ case 'clustered' :
104
+ currentUrl . searchParams . set ( 'dl' , 'clustered' ) ;
105
+ break ;
106
+ case 'oss/enterprise' :
107
+ currentUrl . searchParams . set ( 'dl' , 'oss' ) ;
108
+ break ;
109
+ case 'other' :
110
+ default :
111
+ // No tracking parameter for other/unknown products
112
+ break ;
113
+ }
114
+
115
+ // Add code block specific tracking parameters
116
+ if ( codeBlockInfo . language ) {
117
+ currentUrl . searchParams . set ( 'code_lang' , codeBlockInfo . language ) ;
118
+ }
119
+ if ( codeBlockInfo . lineCount ) {
120
+ currentUrl . searchParams . set ( 'code_lines' , codeBlockInfo . lineCount ) ;
121
+ }
122
+ if ( codeBlockInfo . hasPlaceholders ) {
123
+ currentUrl . searchParams . set ( 'has_placeholders' , 'true' ) ;
124
+ }
125
+ if ( codeBlockInfo . blockType ) {
126
+ currentUrl . searchParams . set ( 'code_type' , codeBlockInfo . blockType ) ;
127
+ }
128
+ if ( codeBlockInfo . sectionTitle ) {
129
+ currentUrl . searchParams . set (
130
+ 'section' ,
131
+ encodeURIComponent ( codeBlockInfo . sectionTitle )
132
+ ) ;
133
+ }
134
+ if ( codeBlockInfo . firstLine ) {
135
+ currentUrl . searchParams . set (
136
+ 'first_line' ,
137
+ encodeURIComponent ( codeBlockInfo . firstLine . substring ( 0 , 100 ) )
138
+ ) ;
139
+ }
140
+
141
+ // Update browser history without triggering page reload
142
+ if ( window . history && window . history . replaceState ) {
143
+ window . history . replaceState ( null , '' , currentUrl . toString ( ) ) ;
144
+ }
145
+
146
+ // Send custom Google Analytics event if gtag is available
147
+ if ( typeof window . gtag !== 'undefined' ) {
148
+ window . gtag ( 'event' , 'code_copy' , {
149
+ language : codeBlockInfo . language ,
150
+ line_count : codeBlockInfo . lineCount ,
151
+ has_placeholders : codeBlockInfo . hasPlaceholders ,
152
+ dl : codeBlockInfo . dl || null ,
153
+ section_title : codeBlockInfo . sectionTitle ,
154
+ first_line : codeBlockInfo . firstLine
155
+ ? codeBlockInfo . firstLine . substring ( 0 , 100 )
156
+ : null ,
157
+ product : context ,
158
+ } ) ;
159
+ }
74
160
75
161
const copyContent = async ( ) => {
76
162
try {
@@ -84,6 +170,71 @@ function initialize() {
84
170
copyContent ( ) ;
85
171
} ) ;
86
172
173
+ /**
174
+ * Extract contextual information about a code block
175
+ * @param {HTMLElement } codeElement - The code block element
176
+ * @returns {Object } Information about the code block
177
+ */
178
+ function extractCodeBlockInfo ( codeElement ) {
179
+ const codeTag = codeElement . querySelector ( 'code' ) ;
180
+ const info = {
181
+ language : null ,
182
+ lineCount : 0 ,
183
+ hasPlaceholders : false ,
184
+ blockType : 'code' ,
185
+ dl : null , // Download script type
186
+ sectionTitle : null ,
187
+ firstLine : null ,
188
+ } ;
189
+
190
+ // Extract language from class attribute
191
+ if ( codeTag && codeTag . className ) {
192
+ const langMatch = codeTag . className . match (
193
+ / l a n g u a g e - ( \w + ) | h l j s - ( \w + ) | ( \w + ) /
194
+ ) ;
195
+ if ( langMatch ) {
196
+ info . language = langMatch [ 1 ] || langMatch [ 2 ] || langMatch [ 3 ] ;
197
+ }
198
+ }
199
+
200
+ // Count lines
201
+ const text = codeElement . innerText || '' ;
202
+ const lines = text . split ( '\n' ) ;
203
+ info . lineCount = lines . length ;
204
+
205
+ // Get first non-empty line
206
+ info . firstLine = lines . find ( ( line ) => line . trim ( ) !== '' ) || null ;
207
+
208
+ // Check for placeholders (common patterns)
209
+ info . hasPlaceholders =
210
+ / \b [ A - Z _ ] { 2 , } \b | \{ \{ [ ^ } ] + \} \} | \$ \{ [ ^ } ] + \} | < [ ^ > ] + > / . test ( text ) ;
211
+
212
+ // Determine if this is a download script
213
+ if ( text . includes ( 'https://www.influxdata.com/d/install_influxdb3.sh' ) ) {
214
+ if ( text . includes ( 'install_influxdb3.sh enterprise' ) ) {
215
+ info . dl = 'enterprise' ;
216
+ } else {
217
+ info . dl = 'oss3' ;
218
+ }
219
+ } else if ( text . includes ( 'docker pull influxdb:3-enterprise' ) ) {
220
+ info . dl = 'enterprise' ;
221
+ } else if ( text . includes ( 'docker pull influxdb3-core' ) ) {
222
+ info . dl = 'oss3' ;
223
+ }
224
+
225
+ // Find nearest section heading
226
+ let element = codeElement ;
227
+ while ( element && element !== document . body ) {
228
+ element = element . previousElementSibling || element . parentElement ;
229
+ if ( element && element . tagName && / ^ H [ 1 - 6 ] $ / . test ( element . tagName ) ) {
230
+ info . sectionTitle = element . textContent . trim ( ) ;
231
+ break ;
232
+ }
233
+ }
234
+
235
+ return info ;
236
+ }
237
+
87
238
/////////////////////////////// FULL WINDOW CODE ///////////////////////////////
88
239
89
240
/*
0 commit comments