diff --git a/integrations/chatwoot/src/referral.js b/integrations/chatwoot/src/referral.js index 9c43ef8..71a0ad9 100644 --- a/integrations/chatwoot/src/referral.js +++ b/integrations/chatwoot/src/referral.js @@ -127,8 +127,11 @@ export function mergeMetaWhatsAppReferralAttributes( if (options?.consentState !== 'granted') return base; const candidate = metaWhatsAppReferralAttributes(referral); - const hasExistingReferral = Object.keys(base).some( - (key) => key.startsWith(META_WHATSAPP_REFERRAL_ATTRIBUTE_PREFIX) && hasValue(base[key]), + const hasExistingReferral = META_WHATSAPP_REFERRAL_FIELDS.some( + (key) => Object.prototype.hasOwnProperty.call( + base, + `${META_WHATSAPP_REFERRAL_ATTRIBUTE_PREFIX}${key}`, + ), ); if (hasExistingReferral) return base; diff --git a/integrations/chatwoot/tests/referral.test.mjs b/integrations/chatwoot/tests/referral.test.mjs index 168d115..a8ac912 100644 --- a/integrations/chatwoot/tests/referral.test.mjs +++ b/integrations/chatwoot/tests/referral.test.mjs @@ -119,3 +119,11 @@ test('preserves an existing flat referral snapshot without mixing messages', () assert.equal(result.clicktrail_referral_ctwa_clid, undefined); assert.equal(result.clicktrail_referral_arbitrary, undefined); }); + +test('treats a recognized flat referral key as occupied regardless of value type', () => { + const existing = { clicktrail_referral_source_id: 42 }; + assert.deepEqual( + mergeMetaWhatsAppReferralAttributes(existing, referral, { consentState: 'granted' }), + existing, + ); +});