@akson/cortex-landing-analytics
Version:
Enhanced analytics for landing pages with lead scoring, multi-channel conversion tracking, and A/B testing support
122 lines (119 loc) • 3.49 kB
JavaScript
// src/types.ts
var LEAD_GENERATION_EVENTS = {
LEAD_PAGE_VIEW: "lead_page_view",
LEAD_INQUIRY_STARTED: "lead_inquiry_started",
LEAD_QUALIFICATION: "lead_qualification",
LEAD_CONTACT_INFO: "lead_contact_info",
LEAD_FORM_SUBMITTED: "lead_form_submitted",
LEAD_WHATSAPP_CONTACT: "lead_whatsapp_contact",
LEAD_FORM_STEP1_VIEWED: "lead_form_step1_viewed",
LEAD_FORM_STEP1_COMPLETED: "lead_form_step1_completed",
LEAD_FORM_STEP2_VIEWED: "lead_form_step2_viewed",
LEAD_FORM_STEP2_COMPLETED: "lead_form_step2_completed",
LEAD_FORM_STEP3_VIEWED: "lead_form_step3_viewed",
LEAD_FORM_STEP3_COMPLETED: "lead_form_step3_completed",
LEAD_FORM_STEP4_VIEWED: "lead_form_step4_viewed",
LEAD_FORM_STEP4_COMPLETED: "lead_form_step4_completed"
};
var LEAD_SCORES = {
LEAD_PAGE_VIEW: 5,
LEAD_CONTENT_VIEW: 15,
LEAD_INQUIRY_STARTED: 40,
LEAD_CONTACT_INFO: 60,
LEAD_WHATSAPP_CONTACT: 85,
LEAD_FORM_SUBMITTED: 100
};
var ECOMMERCE_EVENTS = {
VIEW_ITEM: "view_item",
ADD_TO_CART: "add_to_cart",
BEGIN_CHECKOUT: "begin_checkout",
PURCHASE: "purchase"
};
// src/lead-scoring/index.ts
var LeadScoring = class {
constructor() {
this.profiles = /* @__PURE__ */ new Map();
}
/**
* Track a lead generation event and update scoring
*/
trackEvent(sessionId, event, metadata) {
const eventValue = LEAD_GENERATION_EVENTS[event];
const score = LEAD_SCORES[eventValue] || 0;
const scoreEvent = {
event: eventValue,
score,
timestamp: /* @__PURE__ */ new Date(),
sessionId,
metadata
};
let profile = this.profiles.get(sessionId);
if (!profile) {
profile = {
sessionId,
totalScore: 0,
events: [],
level: "cold",
createdAt: /* @__PURE__ */ new Date(),
updatedAt: /* @__PURE__ */ new Date()
};
this.profiles.set(sessionId, profile);
}
profile.events.push(scoreEvent);
profile.totalScore += score;
profile.updatedAt = /* @__PURE__ */ new Date();
profile.level = this.calculateLevel(profile.totalScore);
return profile;
}
/**
* Get lead profile by session ID
*/
getProfile(sessionId) {
return this.profiles.get(sessionId);
}
/**
* Get all profiles with minimum score
*/
getQualifiedLeads(minScore = 60) {
return Array.from(this.profiles.values()).filter((profile) => profile.totalScore >= minScore).sort((a, b) => b.totalScore - a.totalScore);
}
/**
* Calculate lead level based on score
*/
calculateLevel(score) {
if (score >= 85) return "qualified";
if (score >= 60) return "hot";
if (score >= 30) return "warm";
return "cold";
}
/**
* Export lead data for analytics platforms
*/
exportLeadData(sessionId) {
const profile = this.getProfile(sessionId);
if (!profile) return null;
return {
session_id: profile.sessionId,
user_id: profile.userId,
lead_score: profile.totalScore,
lead_level: profile.level,
total_events: profile.events.length,
first_interaction: profile.createdAt.toISOString(),
last_interaction: profile.updatedAt.toISOString(),
events: profile.events.map((event) => ({
event_name: event.event,
score: event.score,
timestamp: event.timestamp.toISOString(),
metadata: event.metadata
}))
};
}
};
var leadScoring = new LeadScoring();
export {
LEAD_GENERATION_EVENTS,
LEAD_SCORES,
ECOMMERCE_EVENTS,
LeadScoring,
leadScoring
};