@clianta/sdk 1.4.0 → 1.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -225,7 +225,7 @@ declare class CRMClient {
225
225
  * The contact is upserted in the CRM and matching workflow automations fire automatically.
226
226
  *
227
227
  * @example
228
- * const crm = new CRMClient('https://api.clianta.online', 'WORKSPACE_ID');
228
+ * const crm = new CRMClient('http://localhost:5000', 'WORKSPACE_ID');
229
229
  * crm.setApiKey('mm_live_...');
230
230
  *
231
231
  * await crm.sendEvent({
@@ -477,6 +477,86 @@ declare class CRMClient {
477
477
  }): Promise<ApiResponse<{
478
478
  messageId: string;
479
479
  }>>;
480
+ /**
481
+ * Get a contact by email address.
482
+ * Returns the first matching contact from a search query.
483
+ */
484
+ getContactByEmail(email: string): Promise<ApiResponse<PaginatedResponse<Contact>>>;
485
+ /**
486
+ * Get activity timeline for a contact
487
+ */
488
+ getContactActivity(contactId: string, params?: {
489
+ page?: number;
490
+ limit?: number;
491
+ type?: string;
492
+ startDate?: string;
493
+ endDate?: string;
494
+ }): Promise<ApiResponse<PaginatedResponse<Activity>>>;
495
+ /**
496
+ * Get engagement metrics for a contact (via their linked visitor data)
497
+ */
498
+ getContactEngagement(contactId: string): Promise<ApiResponse<{
499
+ totalTimeOnSiteSeconds: number;
500
+ averageSessionDurationSeconds: number;
501
+ totalPageViews: number;
502
+ totalSessions: number;
503
+ engagementScore: number;
504
+ lastActiveAt: string;
505
+ }>>;
506
+ /**
507
+ * Get a full timeline for a contact including events, activities, and opportunities
508
+ */
509
+ getContactTimeline(contactId: string, params?: {
510
+ page?: number;
511
+ limit?: number;
512
+ }): Promise<ApiResponse<PaginatedResponse<{
513
+ type: 'event' | 'activity' | 'opportunity' | 'note';
514
+ title: string;
515
+ description?: string;
516
+ timestamp: string;
517
+ metadata?: Record<string, unknown>;
518
+ }>>>;
519
+ /**
520
+ * Search contacts with advanced filters
521
+ */
522
+ searchContacts(query: string, filters?: {
523
+ status?: string;
524
+ lifecycleStage?: string;
525
+ source?: string;
526
+ tags?: string[];
527
+ page?: number;
528
+ limit?: number;
529
+ }): Promise<ApiResponse<PaginatedResponse<Contact>>>;
530
+ /**
531
+ * List all webhook subscriptions
532
+ */
533
+ listWebhooks(params?: {
534
+ page?: number;
535
+ limit?: number;
536
+ }): Promise<ApiResponse<PaginatedResponse<{
537
+ _id: string;
538
+ url: string;
539
+ events: string[];
540
+ isActive: boolean;
541
+ createdAt: string;
542
+ }>>>;
543
+ /**
544
+ * Create a new webhook subscription
545
+ */
546
+ createWebhook(data: {
547
+ url: string;
548
+ events: string[];
549
+ secret?: string;
550
+ }): Promise<ApiResponse<{
551
+ _id: string;
552
+ url: string;
553
+ events: string[];
554
+ isActive: boolean;
555
+ }>>;
556
+ /**
557
+ * Delete a webhook subscription
558
+ */
559
+ deleteWebhook(webhookId: string): Promise<ApiResponse<void>>;
480
560
  /**
481
561
  * Get all event triggers
482
562
  */
@@ -528,6 +608,8 @@ interface CliantaConfig {
528
608
  useCookies?: boolean;
529
609
  /** Cookie-less mode: use sessionStorage only (no persistent storage) */
530
610
  cookielessMode?: boolean;
611
+ /** Queue persistence mode: 'session' (default), 'local' (survives browser restart), 'none' */
612
+ persistMode?: 'session' | 'local' | 'none';
531
613
  }
532
614
  type PluginName = 'pageView' | 'forms' | 'scroll' | 'clicks' | 'engagement' | 'downloads' | 'exitIntent' | 'errors' | 'performance' | 'popupForms';
533
615
  interface ConsentConfig {
@@ -631,8 +713,34 @@ interface TrackerCore {
631
713
  deleteData(): void;
632
714
  /** Get current consent state */
633
715
  getConsentState(): ConsentState;
716
+ /** Get the current visitor's profile from the CRM */
717
+ getVisitorProfile(): Promise<VisitorProfile | null>;
718
+ /** Get the current visitor's recent activity */
719
+ getVisitorActivity(options?: VisitorActivityOptions): Promise<{
720
+ data: VisitorActivity[];
721
+ pagination: {
722
+ page: number;
723
+ limit: number;
724
+ total: number;
725
+ pages: number;
726
+ };
727
+ } | null>;
728
+ /** Get a summarized journey timeline for the current visitor */
729
+ getVisitorTimeline(): Promise<VisitorTimeline | null>;
730
+ /** Get engagement metrics for the current visitor */
731
+ getVisitorEngagement(): Promise<EngagementMetrics | null>;
634
732
  /** Send a server-side inbound event (requires apiKey in config) */
635
733
  sendEvent(payload: InboundEventPayload): Promise<InboundEventResult>;
734
+ /** Create or update a contact by email (upsert) */
735
+ createContact(data: PublicContactData): Promise<PublicCrmResult>;
736
+ /** Update an existing contact by ID (limited fields) */
737
+ updateContact(contactId: string, data: PublicContactUpdate): Promise<PublicCrmResult>;
738
+ /** Submit a form — creates/updates contact from form data */
739
+ submitForm(formId: string, data: PublicFormSubmission): Promise<PublicCrmResult>;
740
+ /** Log an activity linked to a contact (append-only) */
741
+ logActivity(data: PublicActivityData): Promise<PublicCrmResult>;
742
+ /** Create an opportunity (e.g., from "Request Demo" forms) */
743
+ createOpportunity(data: PublicOpportunityData): Promise<PublicCrmResult>;
636
744
  }
637
745
  interface Contact {
638
746
  _id?: string;
@@ -892,11 +1000,139 @@ interface TriggerExecution {
892
1000
  /** Execution timestamp */
893
1001
  executedAt: string;
894
1002
  }
895
-
896
- /**
897
- * Clianta SDK - Main Tracker Class
898
- * @see SDK_VERSION in core/config.ts
899
- */
1003
+ interface VisitorProfile {
1004
+ visitorId: string;
1005
+ contactId?: string;
1006
+ email?: string;
1007
+ firstName?: string;
1008
+ lastName?: string;
1009
+ company?: string;
1010
+ jobTitle?: string;
1011
+ phone?: string;
1012
+ status?: string;
1013
+ lifecycleStage?: string;
1014
+ tags?: string[];
1015
+ leadScore?: number;
1016
+ firstSeen?: string;
1017
+ lastSeen?: string;
1018
+ sessionCount?: number;
1019
+ pageViewCount?: number;
1020
+ totalTimeSpent?: number;
1021
+ customFields?: Record<string, unknown>;
1022
+ }
1023
+ interface VisitorActivity {
1024
+ _id?: string;
1025
+ eventType: string;
1026
+ eventName: string;
1027
+ url: string;
1028
+ properties?: Record<string, unknown>;
1029
+ timestamp: string;
1030
+ }
1031
+ interface VisitorTimeline {
1032
+ visitorId: string;
1033
+ contactId?: string;
1034
+ firstSeen: string;
1035
+ lastSeen: string;
1036
+ totalSessions: number;
1037
+ totalPageViews: number;
1038
+ totalEvents: number;
1039
+ totalTimeSpentSeconds: number;
1040
+ averageSessionDurationSeconds: number;
1041
+ topPages: Array<{
1042
+ url: string;
1043
+ views: number;
1044
+ avgTimeSeconds?: number;
1045
+ }>;
1046
+ recentActivities: VisitorActivity[];
1047
+ devices: Array<{
1048
+ userAgent: string;
1049
+ lastSeen: string;
1050
+ }>;
1051
+ }
1052
+ interface EngagementMetrics {
1053
+ visitorId: string;
1054
+ totalTimeOnSiteSeconds: number;
1055
+ averageSessionDurationSeconds: number;
1056
+ totalPageViews: number;
1057
+ totalSessions: number;
1058
+ engagementScore: number;
1059
+ bounceRate: number;
1060
+ lastActiveAt: string;
1061
+ topEvents: Array<{
1062
+ eventType: string;
1063
+ count: number;
1064
+ }>;
1065
+ }
1066
+ interface VisitorActivityOptions {
1067
+ page?: number;
1068
+ limit?: number;
1069
+ eventType?: string;
1070
+ startDate?: string;
1071
+ endDate?: string;
1072
+ }
1073
+ interface ContactTimelineOptions {
1074
+ page?: number;
1075
+ limit?: number;
1076
+ includeEvents?: boolean;
1077
+ includeActivities?: boolean;
1078
+ includeOpportunities?: boolean;
1079
+ }
1080
+ interface PublicContactData {
1081
+ email: string;
1082
+ firstName?: string;
1083
+ lastName?: string;
1084
+ company?: string;
1085
+ jobTitle?: string;
1086
+ phone?: string;
1087
+ source?: string;
1088
+ tags?: string[];
1089
+ customFields?: Record<string, unknown>;
1090
+ }
1091
+ interface PublicContactUpdate {
1092
+ firstName?: string;
1093
+ lastName?: string;
1094
+ company?: string;
1095
+ jobTitle?: string;
1096
+ phone?: string;
1097
+ tags?: string[];
1098
+ customFields?: Record<string, unknown>;
1099
+ }
1100
+ interface PublicActivityData {
1101
+ contactId: string;
1102
+ type: 'call' | 'email' | 'meeting' | 'note' | 'other';
1103
+ title: string;
1104
+ description?: string;
1105
+ direction?: 'inbound' | 'outbound';
1106
+ duration?: number;
1107
+ emailSubject?: string;
1108
+ metadata?: Record<string, unknown>;
1109
+ }
1110
+ interface PublicOpportunityData {
1111
+ title: string;
1112
+ contactId: string;
1113
+ pipelineId: string;
1114
+ stageId: string;
1115
+ value?: number;
1116
+ currency?: string;
1117
+ description?: string;
1118
+ expectedCloseDate?: string;
1119
+ customFields?: Record<string, unknown>;
1120
+ }
1121
+ interface PublicFormSubmission {
1122
+ fields: Record<string, unknown>;
1123
+ metadata?: {
1124
+ visitorId?: string;
1125
+ sessionId?: string;
1126
+ pageUrl?: string;
1127
+ referrer?: string;
1128
+ };
1129
+ }
1130
+ interface PublicCrmResult {
1131
+ success: boolean;
1132
+ data?: Record<string, unknown>;
1133
+ error?: string;
1134
+ status?: number;
1135
+ }
900
1136
 
901
1137
  /**
902
1138
  * Main Clianta Tracker Class
@@ -915,6 +1151,8 @@ declare class Tracker implements TrackerCore {
915
1151
  private contactId;
916
1152
  /** Pending identify retry on next flush */
917
1153
  private pendingIdentify;
1154
+ /** Registered event schemas for validation */
1155
+ private eventSchemas;
918
1156
  constructor(workspaceId: string, userConfig?: CliantaConfig);
919
1157
  /**
920
1158
  * Create visitor ID based on storage mode
@@ -952,6 +1190,35 @@ declare class Tracker implements TrackerCore {
952
1190
  * Convenience proxy to CRMClient.sendEvent() — requires apiKey in config.
953
1191
  */
954
1192
  sendEvent(payload: InboundEventPayload): Promise<InboundEventResult>;
1193
+ /**
1194
+ * Get the current visitor's profile from the CRM.
1195
+ * Returns visitor data and linked contact info if identified.
1196
+ * Only returns data for the current visitor (privacy-safe for frontend).
1197
+ */
1198
+ getVisitorProfile(): Promise<VisitorProfile | null>;
1199
+ /**
1200
+ * Get the current visitor's recent activity/events.
1201
+ * Returns paginated list of tracking events for this visitor.
1202
+ */
1203
+ getVisitorActivity(options?: VisitorActivityOptions): Promise<{
1204
+ data: VisitorActivity[];
1205
+ pagination: {
1206
+ page: number;
1207
+ limit: number;
1208
+ total: number;
1209
+ pages: number;
1210
+ };
1211
+ } | null>;
1212
+ /**
1213
+ * Get a summarized journey timeline for the current visitor.
1214
+ * Includes top pages, sessions, time spent, and recent activities.
1215
+ */
1216
+ getVisitorTimeline(): Promise<VisitorTimeline | null>;
1217
+ /**
1218
+ * Get engagement metrics for the current visitor.
1219
+ * Includes time on site, page views, bounce rate, and engagement score.
1220
+ */
1221
+ getVisitorEngagement(): Promise<EngagementMetrics | null>;
955
1222
  /**
956
1223
  * Retry pending identify call
957
1224
  */
@@ -968,6 +1235,22 @@ declare class Tracker implements TrackerCore {
968
1235
  * Toggle debug mode
969
1236
  */
970
1237
  debug(enabled: boolean): void;
1238
+ /**
1239
+ * Register a schema for event validation.
1240
+ * When debug mode is enabled, events will be validated against registered schemas.
1241
+ *
1242
+ * @example
1243
+ * tracker.registerEventSchema('purchase', {
1244
+ * productId: 'string',
1245
+ * price: 'number',
1246
+ * quantity: 'number',
1247
+ * });
1248
+ */
1249
+ registerEventSchema(eventType: string, schema: Record<string, 'string' | 'number' | 'boolean' | 'object' | 'array'>): void;
1250
+ /**
1251
+ * Validate event properties against a registered schema (debug mode only)
1252
+ */
1253
+ private validateEventSchema;
971
1254
  /**
972
1255
  * Get visitor ID
973
1256
  */
@@ -996,6 +1279,31 @@ declare class Tracker implements TrackerCore {
996
1279
  * Delete all stored user data (GDPR right-to-erasure)
997
1280
  */
998
1281
  deleteData(): void;
1282
+ /**
1283
+ * Create or update a contact by email (upsert).
1284
+ * Secured by domain whitelist — no API key needed.
1285
+ */
1286
+ createContact(data: PublicContactData): Promise<PublicCrmResult>;
1287
+ /**
1288
+ * Update an existing contact by ID (limited fields only).
1289
+ */
1290
+ updateContact(contactId: string, data: PublicContactUpdate): Promise<PublicCrmResult>;
1291
+ /**
1292
+ * Submit a form — creates/updates contact from form data.
1293
+ */
1294
+ submitForm(formId: string, data: PublicFormSubmission): Promise<PublicCrmResult>;
1295
+ /**
1296
+ * Log an activity linked to a contact (append-only).
1297
+ */
1298
+ logActivity(data: PublicActivityData): Promise<PublicCrmResult>;
1299
+ /**
1300
+ * Create an opportunity (e.g., from "Request Demo" forms).
1301
+ */
1302
+ createOpportunity(data: PublicOpportunityData): Promise<PublicCrmResult>;
1303
+ /**
1304
+ * Internal helper for public CRM API calls.
1305
+ */
1306
+ private publicCrmRequest;
999
1307
  /**
1000
1308
  * Destroy tracker and cleanup
1001
1309
  */
@@ -1133,4 +1441,4 @@ declare const SDK_VERSION = "1.4.0";
1133
1441
  declare function clianta(workspaceId: string, config?: CliantaConfig): TrackerCore;
1134
1442
 
1135
1443
  export { CRMClient, ConsentManager, EventTriggersManager, SDK_VERSION, Tracker, clianta, clianta as default };
1136
- export type { Activity, ApiResponse, CliantaConfig, Company, ConsentChangeCallback, ConsentConfig, ConsentManagerConfig, ConsentState, Contact, ContactUpdateAction, EmailAction, EmailTemplate, EventTrigger, EventType, InboundEventPayload, InboundEventResult, InboundEventType, Opportunity, PaginatedResponse, Pipeline, PipelineStage, Plugin, PluginName, StoredConsent, Task, TaskAction, TrackerCore, TrackingEvent, TriggerAction, TriggerCondition, TriggerEventType, TriggerExecution, UserTraits, WebhookAction };
1444
+ export type { Activity, ApiResponse, CliantaConfig, Company, ConsentChangeCallback, ConsentConfig, ConsentManagerConfig, ConsentState, Contact, ContactTimelineOptions, ContactUpdateAction, EmailAction, EmailTemplate, EngagementMetrics, EventTrigger, EventType, InboundEventPayload, InboundEventResult, InboundEventType, Opportunity, PaginatedResponse, Pipeline, PipelineStage, Plugin, PluginName, PublicActivityData, PublicContactData, PublicContactUpdate, PublicCrmResult, PublicFormSubmission, PublicOpportunityData, StoredConsent, Task, TaskAction, TrackerCore, TrackingEvent, TriggerAction, TriggerCondition, TriggerEventType, TriggerExecution, UserTraits, VisitorActivity, VisitorActivityOptions, VisitorProfile, VisitorTimeline, WebhookAction };