- The callback dialog switches its copy outside business hours (shared/business-hours.ts); such leads get a "нерабочее время" tag, a marker in the deal name and a line in the note in amoCRM. - utm_*, yclid and gclid are read on page load and kept for 30 days (src/lib/attribution.ts), last tagged visit wins, and are sent with every lead. - The callback dialog is rebuilt: focus trap, focus return, phone masking and a full-number check. - fitnes drops the floating MobileCta; header and mobile bars follow the new dialog state in the other three. - Add the latest design mockups under legacy/. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
99 lines
3.2 KiB
TypeScript
99 lines
3.2 KiB
TypeScript
import { utmKeys } from '../../shared/lead'
|
|
|
|
/**
|
|
* Ad attribution: last tagged visit wins and is kept for 30 days, so a lead
|
|
* sent a week after clicking an ad (with the tags long gone from the URL) is
|
|
* still credited to that campaign. A new tagged visit replaces the whole set —
|
|
* a fresh utm_source is never mixed with an old campaign or click ID.
|
|
*
|
|
* localStorage is the main store, sessionStorage the fallback, and module
|
|
* memory the last resort when the browser refuses both.
|
|
*/
|
|
|
|
type Key = (typeof utmKeys)[number]
|
|
type Values = Partial<Record<Key, string>>
|
|
interface StoredRecord {
|
|
version: 1
|
|
captured_at: number
|
|
values: Values
|
|
}
|
|
|
|
const STORAGE_KEY = 'exo_attribution_v1'
|
|
const TTL_MS = 30 * 24 * 60 * 60 * 1000
|
|
const STORES = ['localStorage', 'sessionStorage'] as const
|
|
|
|
let current: StoredRecord | null = null
|
|
|
|
const isFresh = (record: StoredRecord | null): record is StoredRecord => {
|
|
if (!record) return false
|
|
const age = Date.now() - record.captured_at
|
|
return age >= 0 && age < TTL_MS
|
|
}
|
|
|
|
function clean(values: Record<string, unknown>): Values {
|
|
const out: Values = {}
|
|
for (const key of utmKeys) {
|
|
const value = values[key]
|
|
// The server caps each value at 300 characters; trim here so an oversized
|
|
// tag costs the lead one field, not a 400.
|
|
if (typeof value === 'string' && value.trim()) out[key] = value.trim().slice(0, 300)
|
|
}
|
|
return out
|
|
}
|
|
|
|
function readStored(store: (typeof STORES)[number]): StoredRecord | null {
|
|
try {
|
|
const raw = window[store].getItem(STORAGE_KEY)
|
|
if (!raw) return null
|
|
const parsed = JSON.parse(raw) as Partial<StoredRecord>
|
|
const record: StoredRecord = {
|
|
version: 1,
|
|
captured_at: Number(parsed.captured_at),
|
|
values: clean(parsed.values ?? {}),
|
|
}
|
|
if (parsed.version === 1 && isFresh(record) && Object.keys(record.values).length) return record
|
|
window[store].removeItem(STORAGE_KEY)
|
|
} catch {
|
|
// Blocked storage or corrupt JSON: behave as if nothing was stored.
|
|
}
|
|
return null
|
|
}
|
|
|
|
function persist(record: StoredRecord | null) {
|
|
for (const store of STORES) {
|
|
try {
|
|
if (record) window[store].setItem(STORAGE_KEY, JSON.stringify(record))
|
|
else window[store].removeItem(STORAGE_KEY)
|
|
} catch {
|
|
// See readStored.
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Reads the tags off the current URL, falling back to the stored set. Called
|
|
* once at startup so the visit is captured before any form is touched, and
|
|
* again on every submission.
|
|
*/
|
|
export function collectAttribution(): Values {
|
|
if (!current) {
|
|
const local = readStored('localStorage')
|
|
const session = readStored('sessionStorage')
|
|
current = session && (!local || session.captured_at > local.captured_at) ? session : local
|
|
}
|
|
|
|
const incoming = clean(Object.fromEntries(new URLSearchParams(window.location.search)))
|
|
const sameAsCurrent = current && JSON.stringify(incoming) === JSON.stringify(current.values)
|
|
|
|
// An untagged URL never overwrites a stored source; a tagged one replaces it.
|
|
if (Object.keys(incoming).length && !(sameAsCurrent && isFresh(current))) {
|
|
current = { version: 1, captured_at: Date.now(), values: incoming }
|
|
persist(current)
|
|
} else if (current && !isFresh(current)) {
|
|
current = null
|
|
persist(null)
|
|
}
|
|
|
|
return current ? { ...current.values } : {}
|
|
}
|