|
221 | 221 | debugLog("Error tracking Started Checkout:", err); |
222 | 222 | }); |
223 | 223 | } |
224 | | - function attemptIdentify(source) { |
| 224 | + function attemptIdentify(source, allowReIdentify = false) { |
225 | 225 | if (source) { |
226 | 226 | debugLog("attemptIdentify called from:", source); |
227 | 227 | } |
| 228 | + if (allowReIdentify) { |
| 229 | + debugLog("Re-identification allowed, proceeding"); |
| 230 | + performIdentification(source, true); |
| 231 | + return; |
| 232 | + } |
228 | 233 | if (klaviyo.isIdentified && typeof klaviyo.isIdentified === "function") { |
229 | 234 | klaviyo.isIdentified().then(function(isIdentified) { |
230 | 235 | debugLog("klaviyo.isIdentified():", isIdentified); |
|
244 | 249 | performIdentification(source); |
245 | 250 | } |
246 | 251 | } |
247 | | - function performIdentification(source) { |
| 252 | + function performIdentification(source, forceReIdentify = false) { |
248 | 253 | const iframe = document.querySelector("iframe.mews-distributor") || document.querySelector('iframe[name*="mews-distributor"]'); |
249 | 254 | let searchDoc = document; |
250 | 255 | if (iframe) { |
|
287 | 292 | if (!hasValidPhone && phone) { |
288 | 293 | debugLog("Phone invalid or incomplete:", phone); |
289 | 294 | } |
290 | | - if ((hasValidEmail || hasValidPhone) && !identifyAttempted) { |
| 295 | + const shouldIdentify = (hasValidEmail || hasValidPhone) && (!identifyAttempted || forceReIdentify); |
| 296 | + if (shouldIdentify) { |
291 | 297 | const identifyData = {}; |
292 | 298 | if (hasValidEmail) { |
293 | 299 | identifyData["email"] = email; |
|
304 | 310 | if (lastNameField && lastNameField.value.trim()) { |
305 | 311 | identifyData["last_name"] = lastNameField.value.trim(); |
306 | 312 | } |
307 | | - debugLog("Identifying user with:", identifyData); |
| 313 | + if (forceReIdentify) { |
| 314 | + debugLog("Re-identifying user with:", identifyData); |
| 315 | + } else { |
| 316 | + debugLog("Identifying user with:", identifyData); |
| 317 | + } |
308 | 318 | klaviyo.identify(identifyData); |
309 | | - identifyAttempted = true; |
310 | | - debugLog("Identification complete - no further attempts will be made"); |
| 319 | + if (!forceReIdentify) { |
| 320 | + identifyAttempted = true; |
| 321 | + debugLog("Identification complete"); |
| 322 | + } else { |
| 323 | + debugLog("Re-identification complete"); |
| 324 | + } |
311 | 325 | } |
312 | | - } else if (identifyAttempted) { |
| 326 | + } else if (identifyAttempted && !forceReIdentify) { |
313 | 327 | debugLog("User already identified in this session, skipping"); |
314 | 328 | } else { |
315 | 329 | debugLog("No valid email or phone found yet, will retry"); |
|
447 | 461 | for (let i = 0; i < allInputs.length; i++) { |
448 | 462 | (function(input) { |
449 | 463 | input.addEventListener("blur", function() { |
450 | | - debugLog("Form field blur:", input.name || input.type); |
451 | | - setTimeout(function() { |
452 | | - attemptIdentify("field blur: " + (input.name || input.type)); |
453 | | - }, 500); |
| 464 | + const fieldName = input.name || input.type; |
| 465 | + debugLog("Form field blur:", fieldName); |
| 466 | + const isEmailField = input.type === "email" || input.name === "email" || input.id === "email" || input.getAttribute("data-test-id") === "checkout-field-email" || input.getAttribute("autocomplete") === "email"; |
| 467 | + const isPhoneField = input.type === "tel" || input.name === "phone" || input.name === "phoneNumber" || input.id === "phone" || input.getAttribute("data-test-id") === "checkout-field-phone" || input.getAttribute("autocomplete") === "tel"; |
| 468 | + if (isEmailField) { |
| 469 | + debugLog("Email field detected, attempting re-identification"); |
| 470 | + setTimeout(function() { |
| 471 | + attemptIdentify("email blur", true); |
| 472 | + }, 500); |
| 473 | + } else if (isPhoneField) { |
| 474 | + debugLog("Phone field detected, attempting re-identification"); |
| 475 | + setTimeout(function() { |
| 476 | + attemptIdentify("phone blur", true); |
| 477 | + }, 500); |
| 478 | + } else { |
| 479 | + debugLog("Ignoring blur on non-email/phone field:", fieldName); |
| 480 | + } |
454 | 481 | }); |
455 | 482 | input.addEventListener("change", function() { |
456 | | - debugLog("Form field changed:", input.name || input.type); |
457 | | - setTimeout(function() { |
458 | | - attemptIdentify("field change: " + (input.name || input.type)); |
459 | | - }, 500); |
| 483 | + const fieldName = input.name || input.type; |
| 484 | + const isEmailField = input.type === "email" || input.name === "email" || input.id === "email" || input.getAttribute("data-test-id") === "checkout-field-email" || input.getAttribute("autocomplete") === "email"; |
| 485 | + const isPhoneField = input.type === "tel" || input.name === "phone" || input.name === "phoneNumber" || input.id === "phone" || input.getAttribute("data-test-id") === "checkout-field-phone" || input.getAttribute("autocomplete") === "tel"; |
| 486 | + if (isEmailField || isPhoneField) { |
| 487 | + debugLog("Form field changed:", fieldName); |
| 488 | + setTimeout(function() { |
| 489 | + attemptIdentify("field change: " + fieldName, true); |
| 490 | + }, 500); |
| 491 | + } |
460 | 492 | }); |
461 | 493 | })(allInputs[i]); |
462 | 494 | } |
|
0 commit comments