@clianta/sdk 1.6.0 → 1.6.2
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/README.md +86 -470
- package/dist/angular.cjs.js +39 -1337
- package/dist/angular.cjs.js.map +1 -1
- package/dist/angular.d.ts +2 -125
- package/dist/angular.esm.js +39 -1337
- package/dist/angular.esm.js.map +1 -1
- package/dist/clianta.cjs.js +39 -1336
- package/dist/clianta.cjs.js.map +1 -1
- package/dist/clianta.esm.js +40 -1335
- package/dist/clianta.esm.js.map +1 -1
- package/dist/clianta.umd.js +39 -1336
- package/dist/clianta.umd.js.map +1 -1
- package/dist/clianta.umd.min.js +2 -2
- package/dist/clianta.umd.min.js.map +1 -1
- package/dist/index.d.ts +13 -1033
- package/dist/react.cjs.js +58 -1385
- package/dist/react.cjs.js.map +1 -1
- package/dist/react.d.ts +17 -161
- package/dist/react.esm.js +58 -1385
- package/dist/react.esm.js.map +1 -1
- package/dist/svelte.cjs.js +39 -1335
- package/dist/svelte.cjs.js.map +1 -1
- package/dist/svelte.d.ts +2 -123
- package/dist/svelte.esm.js +39 -1335
- package/dist/svelte.esm.js.map +1 -1
- package/dist/vue.cjs.js +39 -1336
- package/dist/vue.cjs.js.map +1 -1
- package/dist/vue.d.ts +2 -124
- package/dist/vue.esm.js +39 -1336
- package/dist/vue.esm.js.map +1 -1
- package/package.json +2 -2
package/dist/clianta.cjs.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* Clianta SDK v1.6.
|
|
2
|
+
* Clianta SDK v1.6.2
|
|
3
3
|
* (c) 2026 Clianta
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -12,7 +12,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
12
12
|
* @see SDK_VERSION in core/config.ts
|
|
13
13
|
*/
|
|
14
14
|
/** SDK Version */
|
|
15
|
-
const SDK_VERSION = '1.6.
|
|
15
|
+
const SDK_VERSION = '1.6.2';
|
|
16
16
|
/** Default API endpoint — reads from env or falls back to localhost */
|
|
17
17
|
const getDefaultApiEndpoint = () => {
|
|
18
18
|
// Build-time env var (works with Next.js, Vite, CRA, etc.)
|
|
@@ -30,7 +30,7 @@ const getDefaultApiEndpoint = () => {
|
|
|
30
30
|
}
|
|
31
31
|
return 'http://localhost:5000';
|
|
32
32
|
};
|
|
33
|
-
/** Core plugins enabled by default */
|
|
33
|
+
/** Core plugins enabled by default — all auto-track with zero config */
|
|
34
34
|
const DEFAULT_PLUGINS = [
|
|
35
35
|
'pageView',
|
|
36
36
|
'forms',
|
|
@@ -39,13 +39,13 @@ const DEFAULT_PLUGINS = [
|
|
|
39
39
|
'engagement',
|
|
40
40
|
'downloads',
|
|
41
41
|
'exitIntent',
|
|
42
|
+
'errors',
|
|
43
|
+
'performance',
|
|
42
44
|
];
|
|
43
45
|
/** Default configuration values */
|
|
44
46
|
const DEFAULT_CONFIG = {
|
|
45
47
|
projectId: '',
|
|
46
48
|
apiEndpoint: getDefaultApiEndpoint(),
|
|
47
|
-
authToken: '',
|
|
48
|
-
apiKey: '',
|
|
49
49
|
debug: false,
|
|
50
50
|
autoPageView: true,
|
|
51
51
|
plugins: DEFAULT_PLUGINS,
|
|
@@ -2511,1268 +2511,6 @@ class ConsentManager {
|
|
|
2511
2511
|
}
|
|
2512
2512
|
}
|
|
2513
2513
|
|
|
2514
|
-
/**
|
|
2515
|
-
* Clianta SDK - Event Triggers Manager
|
|
2516
|
-
* Manages event-driven automation and email notifications
|
|
2517
|
-
*/
|
|
2518
|
-
/**
|
|
2519
|
-
* Event Triggers Manager
|
|
2520
|
-
* Handles event-driven automation based on CRM actions
|
|
2521
|
-
*
|
|
2522
|
-
* Similar to:
|
|
2523
|
-
* - Salesforce: Process Builder, Flow Automation
|
|
2524
|
-
* - HubSpot: Workflows, Email Sequences
|
|
2525
|
-
* - Pipedrive: Workflow Automation
|
|
2526
|
-
*/
|
|
2527
|
-
class EventTriggersManager {
|
|
2528
|
-
constructor(apiEndpoint, workspaceId, authToken) {
|
|
2529
|
-
this.triggers = new Map();
|
|
2530
|
-
this.listeners = new Map();
|
|
2531
|
-
this.apiEndpoint = apiEndpoint;
|
|
2532
|
-
this.workspaceId = workspaceId;
|
|
2533
|
-
this.authToken = authToken;
|
|
2534
|
-
}
|
|
2535
|
-
/**
|
|
2536
|
-
* Set authentication token
|
|
2537
|
-
*/
|
|
2538
|
-
setAuthToken(token) {
|
|
2539
|
-
this.authToken = token;
|
|
2540
|
-
}
|
|
2541
|
-
/**
|
|
2542
|
-
* Make authenticated API request
|
|
2543
|
-
*/
|
|
2544
|
-
async request(endpoint, options = {}) {
|
|
2545
|
-
const url = `${this.apiEndpoint}${endpoint}`;
|
|
2546
|
-
const headers = {
|
|
2547
|
-
'Content-Type': 'application/json',
|
|
2548
|
-
...(options.headers || {}),
|
|
2549
|
-
};
|
|
2550
|
-
if (this.authToken) {
|
|
2551
|
-
headers['Authorization'] = `Bearer ${this.authToken}`;
|
|
2552
|
-
}
|
|
2553
|
-
try {
|
|
2554
|
-
const response = await fetch(url, {
|
|
2555
|
-
...options,
|
|
2556
|
-
headers,
|
|
2557
|
-
});
|
|
2558
|
-
const data = await response.json();
|
|
2559
|
-
if (!response.ok) {
|
|
2560
|
-
return {
|
|
2561
|
-
success: false,
|
|
2562
|
-
error: data.message || 'Request failed',
|
|
2563
|
-
status: response.status,
|
|
2564
|
-
};
|
|
2565
|
-
}
|
|
2566
|
-
return {
|
|
2567
|
-
success: true,
|
|
2568
|
-
data: data.data || data,
|
|
2569
|
-
status: response.status,
|
|
2570
|
-
};
|
|
2571
|
-
}
|
|
2572
|
-
catch (error) {
|
|
2573
|
-
return {
|
|
2574
|
-
success: false,
|
|
2575
|
-
error: error instanceof Error ? error.message : 'Network error',
|
|
2576
|
-
status: 0,
|
|
2577
|
-
};
|
|
2578
|
-
}
|
|
2579
|
-
}
|
|
2580
|
-
// ============================================
|
|
2581
|
-
// TRIGGER MANAGEMENT
|
|
2582
|
-
// ============================================
|
|
2583
|
-
/**
|
|
2584
|
-
* Get all event triggers
|
|
2585
|
-
*/
|
|
2586
|
-
async getTriggers() {
|
|
2587
|
-
return this.request(`/api/workspaces/${this.workspaceId}/triggers`);
|
|
2588
|
-
}
|
|
2589
|
-
/**
|
|
2590
|
-
* Get a single trigger by ID
|
|
2591
|
-
*/
|
|
2592
|
-
async getTrigger(triggerId) {
|
|
2593
|
-
return this.request(`/api/workspaces/${this.workspaceId}/triggers/${triggerId}`);
|
|
2594
|
-
}
|
|
2595
|
-
/**
|
|
2596
|
-
* Create a new event trigger
|
|
2597
|
-
*/
|
|
2598
|
-
async createTrigger(trigger) {
|
|
2599
|
-
const result = await this.request(`/api/workspaces/${this.workspaceId}/triggers`, {
|
|
2600
|
-
method: 'POST',
|
|
2601
|
-
body: JSON.stringify(trigger),
|
|
2602
|
-
});
|
|
2603
|
-
// Cache the trigger locally if successful
|
|
2604
|
-
if (result.success && result.data?._id) {
|
|
2605
|
-
this.triggers.set(result.data._id, result.data);
|
|
2606
|
-
}
|
|
2607
|
-
return result;
|
|
2608
|
-
}
|
|
2609
|
-
/**
|
|
2610
|
-
* Update an existing trigger
|
|
2611
|
-
*/
|
|
2612
|
-
async updateTrigger(triggerId, updates) {
|
|
2613
|
-
const result = await this.request(`/api/workspaces/${this.workspaceId}/triggers/${triggerId}`, {
|
|
2614
|
-
method: 'PUT',
|
|
2615
|
-
body: JSON.stringify(updates),
|
|
2616
|
-
});
|
|
2617
|
-
// Update cache if successful
|
|
2618
|
-
if (result.success && result.data?._id) {
|
|
2619
|
-
this.triggers.set(result.data._id, result.data);
|
|
2620
|
-
}
|
|
2621
|
-
return result;
|
|
2622
|
-
}
|
|
2623
|
-
/**
|
|
2624
|
-
* Delete a trigger
|
|
2625
|
-
*/
|
|
2626
|
-
async deleteTrigger(triggerId) {
|
|
2627
|
-
const result = await this.request(`/api/workspaces/${this.workspaceId}/triggers/${triggerId}`, {
|
|
2628
|
-
method: 'DELETE',
|
|
2629
|
-
});
|
|
2630
|
-
// Remove from cache if successful
|
|
2631
|
-
if (result.success) {
|
|
2632
|
-
this.triggers.delete(triggerId);
|
|
2633
|
-
}
|
|
2634
|
-
return result;
|
|
2635
|
-
}
|
|
2636
|
-
/**
|
|
2637
|
-
* Activate a trigger
|
|
2638
|
-
*/
|
|
2639
|
-
async activateTrigger(triggerId) {
|
|
2640
|
-
return this.updateTrigger(triggerId, { isActive: true });
|
|
2641
|
-
}
|
|
2642
|
-
/**
|
|
2643
|
-
* Deactivate a trigger
|
|
2644
|
-
*/
|
|
2645
|
-
async deactivateTrigger(triggerId) {
|
|
2646
|
-
return this.updateTrigger(triggerId, { isActive: false });
|
|
2647
|
-
}
|
|
2648
|
-
// ============================================
|
|
2649
|
-
// EVENT HANDLING (CLIENT-SIDE)
|
|
2650
|
-
// ============================================
|
|
2651
|
-
/**
|
|
2652
|
-
* Register a local event listener for client-side triggers
|
|
2653
|
-
* This allows immediate client-side reactions to events
|
|
2654
|
-
*/
|
|
2655
|
-
on(eventType, callback) {
|
|
2656
|
-
if (!this.listeners.has(eventType)) {
|
|
2657
|
-
this.listeners.set(eventType, new Set());
|
|
2658
|
-
}
|
|
2659
|
-
this.listeners.get(eventType).add(callback);
|
|
2660
|
-
logger.debug(`Event listener registered: ${eventType}`);
|
|
2661
|
-
}
|
|
2662
|
-
/**
|
|
2663
|
-
* Remove an event listener
|
|
2664
|
-
*/
|
|
2665
|
-
off(eventType, callback) {
|
|
2666
|
-
const listeners = this.listeners.get(eventType);
|
|
2667
|
-
if (listeners) {
|
|
2668
|
-
listeners.delete(callback);
|
|
2669
|
-
}
|
|
2670
|
-
}
|
|
2671
|
-
/**
|
|
2672
|
-
* Emit an event (client-side only)
|
|
2673
|
-
* This will trigger any registered local listeners
|
|
2674
|
-
*/
|
|
2675
|
-
emit(eventType, data) {
|
|
2676
|
-
logger.debug(`Event emitted: ${eventType}`, data);
|
|
2677
|
-
const listeners = this.listeners.get(eventType);
|
|
2678
|
-
if (listeners) {
|
|
2679
|
-
listeners.forEach(callback => {
|
|
2680
|
-
try {
|
|
2681
|
-
callback(data);
|
|
2682
|
-
}
|
|
2683
|
-
catch (error) {
|
|
2684
|
-
logger.error(`Error in event listener for ${eventType}:`, error);
|
|
2685
|
-
}
|
|
2686
|
-
});
|
|
2687
|
-
}
|
|
2688
|
-
}
|
|
2689
|
-
/**
|
|
2690
|
-
* Check if conditions are met for a trigger
|
|
2691
|
-
* Supports dynamic field evaluation including custom fields and nested paths
|
|
2692
|
-
*/
|
|
2693
|
-
evaluateConditions(conditions, data) {
|
|
2694
|
-
if (!conditions || conditions.length === 0) {
|
|
2695
|
-
return true; // No conditions means always fire
|
|
2696
|
-
}
|
|
2697
|
-
return conditions.every(condition => {
|
|
2698
|
-
// Support dot notation for nested fields (e.g., 'customFields.industry')
|
|
2699
|
-
const fieldValue = condition.field.includes('.')
|
|
2700
|
-
? this.getNestedValue(data, condition.field)
|
|
2701
|
-
: data[condition.field];
|
|
2702
|
-
const targetValue = condition.value;
|
|
2703
|
-
switch (condition.operator) {
|
|
2704
|
-
case 'equals':
|
|
2705
|
-
return fieldValue === targetValue;
|
|
2706
|
-
case 'not_equals':
|
|
2707
|
-
return fieldValue !== targetValue;
|
|
2708
|
-
case 'contains':
|
|
2709
|
-
return String(fieldValue).includes(String(targetValue));
|
|
2710
|
-
case 'greater_than':
|
|
2711
|
-
return Number(fieldValue) > Number(targetValue);
|
|
2712
|
-
case 'less_than':
|
|
2713
|
-
return Number(fieldValue) < Number(targetValue);
|
|
2714
|
-
case 'in':
|
|
2715
|
-
return Array.isArray(targetValue) && targetValue.includes(fieldValue);
|
|
2716
|
-
case 'not_in':
|
|
2717
|
-
return Array.isArray(targetValue) && !targetValue.includes(fieldValue);
|
|
2718
|
-
default:
|
|
2719
|
-
return false;
|
|
2720
|
-
}
|
|
2721
|
-
});
|
|
2722
|
-
}
|
|
2723
|
-
/**
|
|
2724
|
-
* Execute actions for a triggered event (client-side preview)
|
|
2725
|
-
* Note: Actual execution happens on the backend
|
|
2726
|
-
*/
|
|
2727
|
-
async executeActions(trigger, data) {
|
|
2728
|
-
logger.info(`Executing actions for trigger: ${trigger.name}`);
|
|
2729
|
-
for (const action of trigger.actions) {
|
|
2730
|
-
try {
|
|
2731
|
-
await this.executeAction(action, data);
|
|
2732
|
-
}
|
|
2733
|
-
catch (error) {
|
|
2734
|
-
logger.error(`Failed to execute action:`, error);
|
|
2735
|
-
}
|
|
2736
|
-
}
|
|
2737
|
-
}
|
|
2738
|
-
/**
|
|
2739
|
-
* Execute a single action
|
|
2740
|
-
*/
|
|
2741
|
-
async executeAction(action, data) {
|
|
2742
|
-
switch (action.type) {
|
|
2743
|
-
case 'send_email':
|
|
2744
|
-
await this.executeSendEmail(action, data);
|
|
2745
|
-
break;
|
|
2746
|
-
case 'webhook':
|
|
2747
|
-
await this.executeWebhook(action, data);
|
|
2748
|
-
break;
|
|
2749
|
-
case 'create_task':
|
|
2750
|
-
await this.executeCreateTask(action, data);
|
|
2751
|
-
break;
|
|
2752
|
-
case 'update_contact':
|
|
2753
|
-
await this.executeUpdateContact(action, data);
|
|
2754
|
-
break;
|
|
2755
|
-
default:
|
|
2756
|
-
logger.warn(`Unknown action type:`, action);
|
|
2757
|
-
}
|
|
2758
|
-
}
|
|
2759
|
-
/**
|
|
2760
|
-
* Execute send email action (via backend API)
|
|
2761
|
-
*/
|
|
2762
|
-
async executeSendEmail(action, data) {
|
|
2763
|
-
logger.debug('Sending email:', action);
|
|
2764
|
-
const payload = {
|
|
2765
|
-
to: this.replaceVariables(action.to, data),
|
|
2766
|
-
subject: action.subject ? this.replaceVariables(action.subject, data) : undefined,
|
|
2767
|
-
body: action.body ? this.replaceVariables(action.body, data) : undefined,
|
|
2768
|
-
templateId: action.templateId,
|
|
2769
|
-
cc: action.cc,
|
|
2770
|
-
bcc: action.bcc,
|
|
2771
|
-
from: action.from,
|
|
2772
|
-
delayMinutes: action.delayMinutes,
|
|
2773
|
-
};
|
|
2774
|
-
await this.request(`/api/workspaces/${this.workspaceId}/emails/send`, {
|
|
2775
|
-
method: 'POST',
|
|
2776
|
-
body: JSON.stringify(payload),
|
|
2777
|
-
});
|
|
2778
|
-
}
|
|
2779
|
-
/**
|
|
2780
|
-
* Execute webhook action
|
|
2781
|
-
*/
|
|
2782
|
-
async executeWebhook(action, data) {
|
|
2783
|
-
logger.debug('Calling webhook:', action.url);
|
|
2784
|
-
const body = action.body ? this.replaceVariables(action.body, data) : JSON.stringify(data);
|
|
2785
|
-
await fetch(action.url, {
|
|
2786
|
-
method: action.method,
|
|
2787
|
-
headers: {
|
|
2788
|
-
'Content-Type': 'application/json',
|
|
2789
|
-
...action.headers,
|
|
2790
|
-
},
|
|
2791
|
-
body,
|
|
2792
|
-
});
|
|
2793
|
-
}
|
|
2794
|
-
/**
|
|
2795
|
-
* Execute create task action
|
|
2796
|
-
*/
|
|
2797
|
-
async executeCreateTask(action, data) {
|
|
2798
|
-
logger.debug('Creating task:', action.title);
|
|
2799
|
-
const dueDate = action.dueDays
|
|
2800
|
-
? new Date(Date.now() + action.dueDays * 24 * 60 * 60 * 1000).toISOString()
|
|
2801
|
-
: undefined;
|
|
2802
|
-
await this.request(`/api/workspaces/${this.workspaceId}/tasks`, {
|
|
2803
|
-
method: 'POST',
|
|
2804
|
-
body: JSON.stringify({
|
|
2805
|
-
title: this.replaceVariables(action.title, data),
|
|
2806
|
-
description: action.description ? this.replaceVariables(action.description, data) : undefined,
|
|
2807
|
-
priority: action.priority,
|
|
2808
|
-
dueDate,
|
|
2809
|
-
assignedTo: action.assignedTo,
|
|
2810
|
-
relatedContactId: typeof data.contactId === 'string' ? data.contactId : undefined,
|
|
2811
|
-
}),
|
|
2812
|
-
});
|
|
2813
|
-
}
|
|
2814
|
-
/**
|
|
2815
|
-
* Execute update contact action
|
|
2816
|
-
*/
|
|
2817
|
-
async executeUpdateContact(action, data) {
|
|
2818
|
-
const contactId = data.contactId || data._id;
|
|
2819
|
-
if (!contactId) {
|
|
2820
|
-
logger.warn('Cannot update contact: no contactId in data');
|
|
2821
|
-
return;
|
|
2822
|
-
}
|
|
2823
|
-
logger.debug('Updating contact:', contactId);
|
|
2824
|
-
await this.request(`/api/workspaces/${this.workspaceId}/contacts/${contactId}`, {
|
|
2825
|
-
method: 'PUT',
|
|
2826
|
-
body: JSON.stringify(action.updates),
|
|
2827
|
-
});
|
|
2828
|
-
}
|
|
2829
|
-
/**
|
|
2830
|
-
* Replace variables in a string template
|
|
2831
|
-
* Supports syntax like {{contact.email}}, {{opportunity.value}}
|
|
2832
|
-
*/
|
|
2833
|
-
replaceVariables(template, data) {
|
|
2834
|
-
return template.replace(/\{\{([^}]+)\}\}/g, (match, path) => {
|
|
2835
|
-
const value = this.getNestedValue(data, path.trim());
|
|
2836
|
-
return value !== undefined ? String(value) : match;
|
|
2837
|
-
});
|
|
2838
|
-
}
|
|
2839
|
-
/**
|
|
2840
|
-
* Get nested value from object using dot notation
|
|
2841
|
-
* Supports dynamic field access including custom fields
|
|
2842
|
-
*/
|
|
2843
|
-
getNestedValue(obj, path) {
|
|
2844
|
-
return path.split('.').reduce((current, key) => {
|
|
2845
|
-
return current !== null && current !== undefined && typeof current === 'object'
|
|
2846
|
-
? current[key]
|
|
2847
|
-
: undefined;
|
|
2848
|
-
}, obj);
|
|
2849
|
-
}
|
|
2850
|
-
/**
|
|
2851
|
-
* Extract all available field paths from a data object
|
|
2852
|
-
* Useful for dynamic field discovery based on platform-specific attributes
|
|
2853
|
-
* @param obj - The data object to extract fields from
|
|
2854
|
-
* @param prefix - Internal use for nested paths
|
|
2855
|
-
* @param maxDepth - Maximum depth to traverse (default: 3)
|
|
2856
|
-
* @returns Array of field paths (e.g., ['email', 'contact.firstName', 'customFields.industry'])
|
|
2857
|
-
*/
|
|
2858
|
-
extractAvailableFields(obj, prefix = '', maxDepth = 3) {
|
|
2859
|
-
if (maxDepth <= 0)
|
|
2860
|
-
return [];
|
|
2861
|
-
const fields = [];
|
|
2862
|
-
for (const key in obj) {
|
|
2863
|
-
if (!obj.hasOwnProperty(key))
|
|
2864
|
-
continue;
|
|
2865
|
-
const value = obj[key];
|
|
2866
|
-
const fieldPath = prefix ? `${prefix}.${key}` : key;
|
|
2867
|
-
fields.push(fieldPath);
|
|
2868
|
-
// Recursively traverse nested objects
|
|
2869
|
-
if (value !== null && typeof value === 'object' && !Array.isArray(value)) {
|
|
2870
|
-
const nestedFields = this.extractAvailableFields(value, fieldPath, maxDepth - 1);
|
|
2871
|
-
fields.push(...nestedFields);
|
|
2872
|
-
}
|
|
2873
|
-
}
|
|
2874
|
-
return fields;
|
|
2875
|
-
}
|
|
2876
|
-
/**
|
|
2877
|
-
* Get available fields from sample data
|
|
2878
|
-
* Helps with dynamic field detection for platform-specific attributes
|
|
2879
|
-
* @param sampleData - Sample data object to analyze
|
|
2880
|
-
* @returns Array of available field paths
|
|
2881
|
-
*/
|
|
2882
|
-
getAvailableFields(sampleData) {
|
|
2883
|
-
return this.extractAvailableFields(sampleData);
|
|
2884
|
-
}
|
|
2885
|
-
// ============================================
|
|
2886
|
-
// HELPER METHODS FOR COMMON PATTERNS
|
|
2887
|
-
// ============================================
|
|
2888
|
-
/**
|
|
2889
|
-
* Create a simple email trigger
|
|
2890
|
-
* Helper method for common use case
|
|
2891
|
-
*/
|
|
2892
|
-
async createEmailTrigger(config) {
|
|
2893
|
-
return this.createTrigger({
|
|
2894
|
-
name: config.name,
|
|
2895
|
-
eventType: config.eventType,
|
|
2896
|
-
conditions: config.conditions,
|
|
2897
|
-
actions: [
|
|
2898
|
-
{
|
|
2899
|
-
type: 'send_email',
|
|
2900
|
-
to: config.to,
|
|
2901
|
-
subject: config.subject,
|
|
2902
|
-
body: config.body,
|
|
2903
|
-
},
|
|
2904
|
-
],
|
|
2905
|
-
isActive: true,
|
|
2906
|
-
});
|
|
2907
|
-
}
|
|
2908
|
-
/**
|
|
2909
|
-
* Create a task creation trigger
|
|
2910
|
-
*/
|
|
2911
|
-
async createTaskTrigger(config) {
|
|
2912
|
-
return this.createTrigger({
|
|
2913
|
-
name: config.name,
|
|
2914
|
-
eventType: config.eventType,
|
|
2915
|
-
conditions: config.conditions,
|
|
2916
|
-
actions: [
|
|
2917
|
-
{
|
|
2918
|
-
type: 'create_task',
|
|
2919
|
-
title: config.taskTitle,
|
|
2920
|
-
description: config.taskDescription,
|
|
2921
|
-
priority: config.priority,
|
|
2922
|
-
dueDays: config.dueDays,
|
|
2923
|
-
},
|
|
2924
|
-
],
|
|
2925
|
-
isActive: true,
|
|
2926
|
-
});
|
|
2927
|
-
}
|
|
2928
|
-
/**
|
|
2929
|
-
* Create a webhook trigger
|
|
2930
|
-
*/
|
|
2931
|
-
async createWebhookTrigger(config) {
|
|
2932
|
-
return this.createTrigger({
|
|
2933
|
-
name: config.name,
|
|
2934
|
-
eventType: config.eventType,
|
|
2935
|
-
conditions: config.conditions,
|
|
2936
|
-
actions: [
|
|
2937
|
-
{
|
|
2938
|
-
type: 'webhook',
|
|
2939
|
-
url: config.webhookUrl,
|
|
2940
|
-
method: config.method || 'POST',
|
|
2941
|
-
},
|
|
2942
|
-
],
|
|
2943
|
-
isActive: true,
|
|
2944
|
-
});
|
|
2945
|
-
}
|
|
2946
|
-
}
|
|
2947
|
-
|
|
2948
|
-
/**
|
|
2949
|
-
* Clianta SDK - CRM API Client
|
|
2950
|
-
* @see SDK_VERSION in core/config.ts
|
|
2951
|
-
*/
|
|
2952
|
-
/**
|
|
2953
|
-
* CRM API Client for managing contacts and opportunities
|
|
2954
|
-
*/
|
|
2955
|
-
class CRMClient {
|
|
2956
|
-
constructor(apiEndpoint, workspaceId, authToken, apiKey) {
|
|
2957
|
-
this.apiEndpoint = apiEndpoint;
|
|
2958
|
-
this.workspaceId = workspaceId;
|
|
2959
|
-
this.authToken = authToken;
|
|
2960
|
-
this.apiKey = apiKey;
|
|
2961
|
-
this.triggers = new EventTriggersManager(apiEndpoint, workspaceId, authToken);
|
|
2962
|
-
}
|
|
2963
|
-
/**
|
|
2964
|
-
* Set authentication token for API requests (user JWT)
|
|
2965
|
-
*/
|
|
2966
|
-
setAuthToken(token) {
|
|
2967
|
-
this.authToken = token;
|
|
2968
|
-
this.apiKey = undefined;
|
|
2969
|
-
this.triggers.setAuthToken(token);
|
|
2970
|
-
}
|
|
2971
|
-
/**
|
|
2972
|
-
* Set workspace API key for server-to-server requests.
|
|
2973
|
-
* Use this instead of setAuthToken when integrating from an external app.
|
|
2974
|
-
*/
|
|
2975
|
-
setApiKey(key) {
|
|
2976
|
-
this.apiKey = key;
|
|
2977
|
-
this.authToken = undefined;
|
|
2978
|
-
}
|
|
2979
|
-
/**
|
|
2980
|
-
* Validate required parameter exists
|
|
2981
|
-
* @throws {Error} if value is null/undefined or empty string
|
|
2982
|
-
*/
|
|
2983
|
-
validateRequired(param, value, methodName) {
|
|
2984
|
-
if (value === null || value === undefined || value === '') {
|
|
2985
|
-
throw new Error(`[CRMClient.${methodName}] ${param} is required`);
|
|
2986
|
-
}
|
|
2987
|
-
}
|
|
2988
|
-
/**
|
|
2989
|
-
* Make authenticated API request
|
|
2990
|
-
*/
|
|
2991
|
-
async request(endpoint, options = {}) {
|
|
2992
|
-
const url = `${this.apiEndpoint}${endpoint}`;
|
|
2993
|
-
const headers = {
|
|
2994
|
-
'Content-Type': 'application/json',
|
|
2995
|
-
...(options.headers || {}),
|
|
2996
|
-
};
|
|
2997
|
-
if (this.apiKey) {
|
|
2998
|
-
headers['X-Api-Key'] = this.apiKey;
|
|
2999
|
-
}
|
|
3000
|
-
else if (this.authToken) {
|
|
3001
|
-
headers['Authorization'] = `Bearer ${this.authToken}`;
|
|
3002
|
-
}
|
|
3003
|
-
try {
|
|
3004
|
-
const response = await fetch(url, {
|
|
3005
|
-
...options,
|
|
3006
|
-
headers,
|
|
3007
|
-
});
|
|
3008
|
-
const data = await response.json();
|
|
3009
|
-
if (!response.ok) {
|
|
3010
|
-
return {
|
|
3011
|
-
success: false,
|
|
3012
|
-
error: data.message || 'Request failed',
|
|
3013
|
-
status: response.status,
|
|
3014
|
-
};
|
|
3015
|
-
}
|
|
3016
|
-
return {
|
|
3017
|
-
success: true,
|
|
3018
|
-
data: data.data || data,
|
|
3019
|
-
status: response.status,
|
|
3020
|
-
};
|
|
3021
|
-
}
|
|
3022
|
-
catch (error) {
|
|
3023
|
-
return {
|
|
3024
|
-
success: false,
|
|
3025
|
-
error: error instanceof Error ? error.message : 'Network error',
|
|
3026
|
-
status: 0,
|
|
3027
|
-
};
|
|
3028
|
-
}
|
|
3029
|
-
}
|
|
3030
|
-
// ============================================
|
|
3031
|
-
// INBOUND EVENTS API (API-key authenticated)
|
|
3032
|
-
// ============================================
|
|
3033
|
-
/**
|
|
3034
|
-
* Send an inbound event from an external app (e.g. user signup on client website).
|
|
3035
|
-
* Requires the client to be initialized with an API key via setApiKey() or the constructor.
|
|
3036
|
-
*
|
|
3037
|
-
* The contact is upserted in the CRM and matching workflow automations fire automatically.
|
|
3038
|
-
*
|
|
3039
|
-
* @example
|
|
3040
|
-
* const crm = new CRMClient('http://localhost:5000', 'WORKSPACE_ID');
|
|
3041
|
-
* crm.setApiKey('mm_live_...');
|
|
3042
|
-
*
|
|
3043
|
-
* await crm.sendEvent({
|
|
3044
|
-
* event: 'user.registered',
|
|
3045
|
-
* contact: { email: 'alice@example.com', firstName: 'Alice' },
|
|
3046
|
-
* data: { plan: 'free', signupSource: 'homepage' },
|
|
3047
|
-
* });
|
|
3048
|
-
*/
|
|
3049
|
-
async sendEvent(payload) {
|
|
3050
|
-
const url = `${this.apiEndpoint}/api/public/events`;
|
|
3051
|
-
const headers = { 'Content-Type': 'application/json' };
|
|
3052
|
-
if (this.apiKey) {
|
|
3053
|
-
headers['X-Api-Key'] = this.apiKey;
|
|
3054
|
-
}
|
|
3055
|
-
else if (this.authToken) {
|
|
3056
|
-
headers['Authorization'] = `Bearer ${this.authToken}`;
|
|
3057
|
-
}
|
|
3058
|
-
try {
|
|
3059
|
-
const response = await fetch(url, {
|
|
3060
|
-
method: 'POST',
|
|
3061
|
-
headers,
|
|
3062
|
-
body: JSON.stringify(payload),
|
|
3063
|
-
});
|
|
3064
|
-
const data = await response.json();
|
|
3065
|
-
if (!response.ok) {
|
|
3066
|
-
return {
|
|
3067
|
-
success: false,
|
|
3068
|
-
contactCreated: false,
|
|
3069
|
-
event: payload.event,
|
|
3070
|
-
error: data.error || 'Request failed',
|
|
3071
|
-
};
|
|
3072
|
-
}
|
|
3073
|
-
return {
|
|
3074
|
-
success: data.success,
|
|
3075
|
-
contactCreated: data.contactCreated,
|
|
3076
|
-
contactId: data.contactId,
|
|
3077
|
-
event: data.event,
|
|
3078
|
-
};
|
|
3079
|
-
}
|
|
3080
|
-
catch (error) {
|
|
3081
|
-
return {
|
|
3082
|
-
success: false,
|
|
3083
|
-
contactCreated: false,
|
|
3084
|
-
event: payload.event,
|
|
3085
|
-
error: error instanceof Error ? error.message : 'Network error',
|
|
3086
|
-
};
|
|
3087
|
-
}
|
|
3088
|
-
}
|
|
3089
|
-
// ============================================
|
|
3090
|
-
// CONTACTS API
|
|
3091
|
-
// ============================================
|
|
3092
|
-
/**
|
|
3093
|
-
* Get all contacts with pagination
|
|
3094
|
-
*/
|
|
3095
|
-
async getContacts(params) {
|
|
3096
|
-
const queryParams = new URLSearchParams();
|
|
3097
|
-
if (params?.page)
|
|
3098
|
-
queryParams.set('page', params.page.toString());
|
|
3099
|
-
if (params?.limit)
|
|
3100
|
-
queryParams.set('limit', params.limit.toString());
|
|
3101
|
-
if (params?.search)
|
|
3102
|
-
queryParams.set('search', params.search);
|
|
3103
|
-
if (params?.status)
|
|
3104
|
-
queryParams.set('status', params.status);
|
|
3105
|
-
const query = queryParams.toString();
|
|
3106
|
-
const endpoint = `/api/workspaces/${this.workspaceId}/contacts${query ? `?${query}` : ''}`;
|
|
3107
|
-
return this.request(endpoint);
|
|
3108
|
-
}
|
|
3109
|
-
/**
|
|
3110
|
-
* Get a single contact by ID
|
|
3111
|
-
*/
|
|
3112
|
-
async getContact(contactId) {
|
|
3113
|
-
this.validateRequired('contactId', contactId, 'getContact');
|
|
3114
|
-
return this.request(`/api/workspaces/${this.workspaceId}/contacts/${contactId}`);
|
|
3115
|
-
}
|
|
3116
|
-
/**
|
|
3117
|
-
* Create a new contact
|
|
3118
|
-
*/
|
|
3119
|
-
async createContact(contact) {
|
|
3120
|
-
return this.request(`/api/workspaces/${this.workspaceId}/contacts`, {
|
|
3121
|
-
method: 'POST',
|
|
3122
|
-
body: JSON.stringify(contact),
|
|
3123
|
-
});
|
|
3124
|
-
}
|
|
3125
|
-
/**
|
|
3126
|
-
* Update an existing contact
|
|
3127
|
-
*/
|
|
3128
|
-
async updateContact(contactId, updates) {
|
|
3129
|
-
this.validateRequired('contactId', contactId, 'updateContact');
|
|
3130
|
-
return this.request(`/api/workspaces/${this.workspaceId}/contacts/${contactId}`, {
|
|
3131
|
-
method: 'PUT',
|
|
3132
|
-
body: JSON.stringify(updates),
|
|
3133
|
-
});
|
|
3134
|
-
}
|
|
3135
|
-
/**
|
|
3136
|
-
* Delete a contact
|
|
3137
|
-
*/
|
|
3138
|
-
async deleteContact(contactId) {
|
|
3139
|
-
this.validateRequired('contactId', contactId, 'deleteContact');
|
|
3140
|
-
return this.request(`/api/workspaces/${this.workspaceId}/contacts/${contactId}`, {
|
|
3141
|
-
method: 'DELETE',
|
|
3142
|
-
});
|
|
3143
|
-
}
|
|
3144
|
-
// ============================================
|
|
3145
|
-
// OPPORTUNITIES API
|
|
3146
|
-
// ============================================
|
|
3147
|
-
/**
|
|
3148
|
-
* Get all opportunities with pagination
|
|
3149
|
-
*/
|
|
3150
|
-
async getOpportunities(params) {
|
|
3151
|
-
const queryParams = new URLSearchParams();
|
|
3152
|
-
if (params?.page)
|
|
3153
|
-
queryParams.set('page', params.page.toString());
|
|
3154
|
-
if (params?.limit)
|
|
3155
|
-
queryParams.set('limit', params.limit.toString());
|
|
3156
|
-
if (params?.pipelineId)
|
|
3157
|
-
queryParams.set('pipelineId', params.pipelineId);
|
|
3158
|
-
if (params?.stageId)
|
|
3159
|
-
queryParams.set('stageId', params.stageId);
|
|
3160
|
-
const query = queryParams.toString();
|
|
3161
|
-
const endpoint = `/api/workspaces/${this.workspaceId}/opportunities${query ? `?${query}` : ''}`;
|
|
3162
|
-
return this.request(endpoint);
|
|
3163
|
-
}
|
|
3164
|
-
/**
|
|
3165
|
-
* Get a single opportunity by ID
|
|
3166
|
-
*/
|
|
3167
|
-
async getOpportunity(opportunityId) {
|
|
3168
|
-
return this.request(`/api/workspaces/${this.workspaceId}/opportunities/${opportunityId}`);
|
|
3169
|
-
}
|
|
3170
|
-
/**
|
|
3171
|
-
* Create a new opportunity
|
|
3172
|
-
*/
|
|
3173
|
-
async createOpportunity(opportunity) {
|
|
3174
|
-
return this.request(`/api/workspaces/${this.workspaceId}/opportunities`, {
|
|
3175
|
-
method: 'POST',
|
|
3176
|
-
body: JSON.stringify(opportunity),
|
|
3177
|
-
});
|
|
3178
|
-
}
|
|
3179
|
-
/**
|
|
3180
|
-
* Update an existing opportunity
|
|
3181
|
-
*/
|
|
3182
|
-
async updateOpportunity(opportunityId, updates) {
|
|
3183
|
-
return this.request(`/api/workspaces/${this.workspaceId}/opportunities/${opportunityId}`, {
|
|
3184
|
-
method: 'PUT',
|
|
3185
|
-
body: JSON.stringify(updates),
|
|
3186
|
-
});
|
|
3187
|
-
}
|
|
3188
|
-
/**
|
|
3189
|
-
* Delete an opportunity
|
|
3190
|
-
*/
|
|
3191
|
-
async deleteOpportunity(opportunityId) {
|
|
3192
|
-
return this.request(`/api/workspaces/${this.workspaceId}/opportunities/${opportunityId}`, {
|
|
3193
|
-
method: 'DELETE',
|
|
3194
|
-
});
|
|
3195
|
-
}
|
|
3196
|
-
/**
|
|
3197
|
-
* Move opportunity to a different stage
|
|
3198
|
-
*/
|
|
3199
|
-
async moveOpportunity(opportunityId, stageId) {
|
|
3200
|
-
return this.request(`/api/workspaces/${this.workspaceId}/opportunities/${opportunityId}/move`, {
|
|
3201
|
-
method: 'POST',
|
|
3202
|
-
body: JSON.stringify({ stageId }),
|
|
3203
|
-
});
|
|
3204
|
-
}
|
|
3205
|
-
// ============================================
|
|
3206
|
-
// COMPANIES API
|
|
3207
|
-
// ============================================
|
|
3208
|
-
/**
|
|
3209
|
-
* Get all companies with pagination
|
|
3210
|
-
*/
|
|
3211
|
-
async getCompanies(params) {
|
|
3212
|
-
const queryParams = new URLSearchParams();
|
|
3213
|
-
if (params?.page)
|
|
3214
|
-
queryParams.set('page', params.page.toString());
|
|
3215
|
-
if (params?.limit)
|
|
3216
|
-
queryParams.set('limit', params.limit.toString());
|
|
3217
|
-
if (params?.search)
|
|
3218
|
-
queryParams.set('search', params.search);
|
|
3219
|
-
if (params?.status)
|
|
3220
|
-
queryParams.set('status', params.status);
|
|
3221
|
-
if (params?.industry)
|
|
3222
|
-
queryParams.set('industry', params.industry);
|
|
3223
|
-
const query = queryParams.toString();
|
|
3224
|
-
const endpoint = `/api/workspaces/${this.workspaceId}/companies${query ? `?${query}` : ''}`;
|
|
3225
|
-
return this.request(endpoint);
|
|
3226
|
-
}
|
|
3227
|
-
/**
|
|
3228
|
-
* Get a single company by ID
|
|
3229
|
-
*/
|
|
3230
|
-
async getCompany(companyId) {
|
|
3231
|
-
return this.request(`/api/workspaces/${this.workspaceId}/companies/${companyId}`);
|
|
3232
|
-
}
|
|
3233
|
-
/**
|
|
3234
|
-
* Create a new company
|
|
3235
|
-
*/
|
|
3236
|
-
async createCompany(company) {
|
|
3237
|
-
return this.request(`/api/workspaces/${this.workspaceId}/companies`, {
|
|
3238
|
-
method: 'POST',
|
|
3239
|
-
body: JSON.stringify(company),
|
|
3240
|
-
});
|
|
3241
|
-
}
|
|
3242
|
-
/**
|
|
3243
|
-
* Update an existing company
|
|
3244
|
-
*/
|
|
3245
|
-
async updateCompany(companyId, updates) {
|
|
3246
|
-
return this.request(`/api/workspaces/${this.workspaceId}/companies/${companyId}`, {
|
|
3247
|
-
method: 'PUT',
|
|
3248
|
-
body: JSON.stringify(updates),
|
|
3249
|
-
});
|
|
3250
|
-
}
|
|
3251
|
-
/**
|
|
3252
|
-
* Delete a company
|
|
3253
|
-
*/
|
|
3254
|
-
async deleteCompany(companyId) {
|
|
3255
|
-
return this.request(`/api/workspaces/${this.workspaceId}/companies/${companyId}`, {
|
|
3256
|
-
method: 'DELETE',
|
|
3257
|
-
});
|
|
3258
|
-
}
|
|
3259
|
-
/**
|
|
3260
|
-
* Get contacts belonging to a company
|
|
3261
|
-
*/
|
|
3262
|
-
async getCompanyContacts(companyId, params) {
|
|
3263
|
-
const queryParams = new URLSearchParams();
|
|
3264
|
-
if (params?.page)
|
|
3265
|
-
queryParams.set('page', params.page.toString());
|
|
3266
|
-
if (params?.limit)
|
|
3267
|
-
queryParams.set('limit', params.limit.toString());
|
|
3268
|
-
const query = queryParams.toString();
|
|
3269
|
-
const endpoint = `/api/workspaces/${this.workspaceId}/companies/${companyId}/contacts${query ? `?${query}` : ''}`;
|
|
3270
|
-
return this.request(endpoint);
|
|
3271
|
-
}
|
|
3272
|
-
/**
|
|
3273
|
-
* Get deals/opportunities belonging to a company
|
|
3274
|
-
*/
|
|
3275
|
-
async getCompanyDeals(companyId, params) {
|
|
3276
|
-
const queryParams = new URLSearchParams();
|
|
3277
|
-
if (params?.page)
|
|
3278
|
-
queryParams.set('page', params.page.toString());
|
|
3279
|
-
if (params?.limit)
|
|
3280
|
-
queryParams.set('limit', params.limit.toString());
|
|
3281
|
-
const query = queryParams.toString();
|
|
3282
|
-
const endpoint = `/api/workspaces/${this.workspaceId}/companies/${companyId}/deals${query ? `?${query}` : ''}`;
|
|
3283
|
-
return this.request(endpoint);
|
|
3284
|
-
}
|
|
3285
|
-
// ============================================
|
|
3286
|
-
// PIPELINES API
|
|
3287
|
-
// ============================================
|
|
3288
|
-
/**
|
|
3289
|
-
* Get all pipelines
|
|
3290
|
-
*/
|
|
3291
|
-
async getPipelines() {
|
|
3292
|
-
return this.request(`/api/workspaces/${this.workspaceId}/pipelines`);
|
|
3293
|
-
}
|
|
3294
|
-
/**
|
|
3295
|
-
* Get a single pipeline by ID
|
|
3296
|
-
*/
|
|
3297
|
-
async getPipeline(pipelineId) {
|
|
3298
|
-
return this.request(`/api/workspaces/${this.workspaceId}/pipelines/${pipelineId}`);
|
|
3299
|
-
}
|
|
3300
|
-
/**
|
|
3301
|
-
* Create a new pipeline
|
|
3302
|
-
*/
|
|
3303
|
-
async createPipeline(pipeline) {
|
|
3304
|
-
return this.request(`/api/workspaces/${this.workspaceId}/pipelines`, {
|
|
3305
|
-
method: 'POST',
|
|
3306
|
-
body: JSON.stringify(pipeline),
|
|
3307
|
-
});
|
|
3308
|
-
}
|
|
3309
|
-
/**
|
|
3310
|
-
* Update an existing pipeline
|
|
3311
|
-
*/
|
|
3312
|
-
async updatePipeline(pipelineId, updates) {
|
|
3313
|
-
return this.request(`/api/workspaces/${this.workspaceId}/pipelines/${pipelineId}`, {
|
|
3314
|
-
method: 'PUT',
|
|
3315
|
-
body: JSON.stringify(updates),
|
|
3316
|
-
});
|
|
3317
|
-
}
|
|
3318
|
-
/**
|
|
3319
|
-
* Delete a pipeline
|
|
3320
|
-
*/
|
|
3321
|
-
async deletePipeline(pipelineId) {
|
|
3322
|
-
return this.request(`/api/workspaces/${this.workspaceId}/pipelines/${pipelineId}`, {
|
|
3323
|
-
method: 'DELETE',
|
|
3324
|
-
});
|
|
3325
|
-
}
|
|
3326
|
-
// ============================================
|
|
3327
|
-
// TASKS API
|
|
3328
|
-
// ============================================
|
|
3329
|
-
/**
|
|
3330
|
-
* Get all tasks with pagination
|
|
3331
|
-
*/
|
|
3332
|
-
async getTasks(params) {
|
|
3333
|
-
const queryParams = new URLSearchParams();
|
|
3334
|
-
if (params?.page)
|
|
3335
|
-
queryParams.set('page', params.page.toString());
|
|
3336
|
-
if (params?.limit)
|
|
3337
|
-
queryParams.set('limit', params.limit.toString());
|
|
3338
|
-
if (params?.status)
|
|
3339
|
-
queryParams.set('status', params.status);
|
|
3340
|
-
if (params?.priority)
|
|
3341
|
-
queryParams.set('priority', params.priority);
|
|
3342
|
-
if (params?.contactId)
|
|
3343
|
-
queryParams.set('contactId', params.contactId);
|
|
3344
|
-
if (params?.companyId)
|
|
3345
|
-
queryParams.set('companyId', params.companyId);
|
|
3346
|
-
if (params?.opportunityId)
|
|
3347
|
-
queryParams.set('opportunityId', params.opportunityId);
|
|
3348
|
-
const query = queryParams.toString();
|
|
3349
|
-
const endpoint = `/api/workspaces/${this.workspaceId}/tasks${query ? `?${query}` : ''}`;
|
|
3350
|
-
return this.request(endpoint);
|
|
3351
|
-
}
|
|
3352
|
-
/**
|
|
3353
|
-
* Get a single task by ID
|
|
3354
|
-
*/
|
|
3355
|
-
async getTask(taskId) {
|
|
3356
|
-
return this.request(`/api/workspaces/${this.workspaceId}/tasks/${taskId}`);
|
|
3357
|
-
}
|
|
3358
|
-
/**
|
|
3359
|
-
* Create a new task
|
|
3360
|
-
*/
|
|
3361
|
-
async createTask(task) {
|
|
3362
|
-
return this.request(`/api/workspaces/${this.workspaceId}/tasks`, {
|
|
3363
|
-
method: 'POST',
|
|
3364
|
-
body: JSON.stringify(task),
|
|
3365
|
-
});
|
|
3366
|
-
}
|
|
3367
|
-
/**
|
|
3368
|
-
* Update an existing task
|
|
3369
|
-
*/
|
|
3370
|
-
async updateTask(taskId, updates) {
|
|
3371
|
-
return this.request(`/api/workspaces/${this.workspaceId}/tasks/${taskId}`, {
|
|
3372
|
-
method: 'PUT',
|
|
3373
|
-
body: JSON.stringify(updates),
|
|
3374
|
-
});
|
|
3375
|
-
}
|
|
3376
|
-
/**
|
|
3377
|
-
* Mark a task as completed
|
|
3378
|
-
*/
|
|
3379
|
-
async completeTask(taskId) {
|
|
3380
|
-
return this.request(`/api/workspaces/${this.workspaceId}/tasks/${taskId}/complete`, {
|
|
3381
|
-
method: 'PATCH',
|
|
3382
|
-
});
|
|
3383
|
-
}
|
|
3384
|
-
/**
|
|
3385
|
-
* Delete a task
|
|
3386
|
-
*/
|
|
3387
|
-
async deleteTask(taskId) {
|
|
3388
|
-
return this.request(`/api/workspaces/${this.workspaceId}/tasks/${taskId}`, {
|
|
3389
|
-
method: 'DELETE',
|
|
3390
|
-
});
|
|
3391
|
-
}
|
|
3392
|
-
// ============================================
|
|
3393
|
-
// ACTIVITIES API
|
|
3394
|
-
// ============================================
|
|
3395
|
-
/**
|
|
3396
|
-
* Get activities for a contact
|
|
3397
|
-
*/
|
|
3398
|
-
async getContactActivities(contactId, params) {
|
|
3399
|
-
const queryParams = new URLSearchParams();
|
|
3400
|
-
if (params?.page)
|
|
3401
|
-
queryParams.set('page', params.page.toString());
|
|
3402
|
-
if (params?.limit)
|
|
3403
|
-
queryParams.set('limit', params.limit.toString());
|
|
3404
|
-
if (params?.type)
|
|
3405
|
-
queryParams.set('type', params.type);
|
|
3406
|
-
const query = queryParams.toString();
|
|
3407
|
-
const endpoint = `/api/workspaces/${this.workspaceId}/contacts/${contactId}/activities${query ? `?${query}` : ''}`;
|
|
3408
|
-
return this.request(endpoint);
|
|
3409
|
-
}
|
|
3410
|
-
/**
|
|
3411
|
-
* Get activities for an opportunity/deal
|
|
3412
|
-
*/
|
|
3413
|
-
async getOpportunityActivities(opportunityId, params) {
|
|
3414
|
-
const queryParams = new URLSearchParams();
|
|
3415
|
-
if (params?.page)
|
|
3416
|
-
queryParams.set('page', params.page.toString());
|
|
3417
|
-
if (params?.limit)
|
|
3418
|
-
queryParams.set('limit', params.limit.toString());
|
|
3419
|
-
if (params?.type)
|
|
3420
|
-
queryParams.set('type', params.type);
|
|
3421
|
-
const query = queryParams.toString();
|
|
3422
|
-
const endpoint = `/api/workspaces/${this.workspaceId}/opportunities/${opportunityId}/activities${query ? `?${query}` : ''}`;
|
|
3423
|
-
return this.request(endpoint);
|
|
3424
|
-
}
|
|
3425
|
-
/**
|
|
3426
|
-
* Create a new activity
|
|
3427
|
-
*/
|
|
3428
|
-
async createActivity(activity) {
|
|
3429
|
-
// Determine the correct endpoint based on related entity
|
|
3430
|
-
let endpoint;
|
|
3431
|
-
if (activity.opportunityId) {
|
|
3432
|
-
endpoint = `/api/workspaces/${this.workspaceId}/opportunities/${activity.opportunityId}/activities`;
|
|
3433
|
-
}
|
|
3434
|
-
else if (activity.contactId) {
|
|
3435
|
-
endpoint = `/api/workspaces/${this.workspaceId}/contacts/${activity.contactId}/activities`;
|
|
3436
|
-
}
|
|
3437
|
-
else {
|
|
3438
|
-
endpoint = `/api/workspaces/${this.workspaceId}/activities`;
|
|
3439
|
-
}
|
|
3440
|
-
return this.request(endpoint, {
|
|
3441
|
-
method: 'POST',
|
|
3442
|
-
body: JSON.stringify(activity),
|
|
3443
|
-
});
|
|
3444
|
-
}
|
|
3445
|
-
/**
|
|
3446
|
-
* Update an existing activity
|
|
3447
|
-
*/
|
|
3448
|
-
async updateActivity(activityId, updates) {
|
|
3449
|
-
return this.request(`/api/workspaces/${this.workspaceId}/activities/${activityId}`, {
|
|
3450
|
-
method: 'PATCH',
|
|
3451
|
-
body: JSON.stringify(updates),
|
|
3452
|
-
});
|
|
3453
|
-
}
|
|
3454
|
-
/**
|
|
3455
|
-
* Delete an activity
|
|
3456
|
-
*/
|
|
3457
|
-
async deleteActivity(activityId) {
|
|
3458
|
-
return this.request(`/api/workspaces/${this.workspaceId}/activities/${activityId}`, {
|
|
3459
|
-
method: 'DELETE',
|
|
3460
|
-
});
|
|
3461
|
-
}
|
|
3462
|
-
/**
|
|
3463
|
-
* Log a call activity
|
|
3464
|
-
*/
|
|
3465
|
-
async logCall(data) {
|
|
3466
|
-
return this.createActivity({
|
|
3467
|
-
type: 'call',
|
|
3468
|
-
title: `${data.direction === 'inbound' ? 'Inbound' : 'Outbound'} Call`,
|
|
3469
|
-
direction: data.direction,
|
|
3470
|
-
duration: data.duration,
|
|
3471
|
-
outcome: data.outcome,
|
|
3472
|
-
description: data.notes,
|
|
3473
|
-
contactId: data.contactId,
|
|
3474
|
-
opportunityId: data.opportunityId,
|
|
3475
|
-
});
|
|
3476
|
-
}
|
|
3477
|
-
/**
|
|
3478
|
-
* Log a meeting activity
|
|
3479
|
-
*/
|
|
3480
|
-
async logMeeting(data) {
|
|
3481
|
-
return this.createActivity({
|
|
3482
|
-
type: 'meeting',
|
|
3483
|
-
title: data.title,
|
|
3484
|
-
duration: data.duration,
|
|
3485
|
-
outcome: data.outcome,
|
|
3486
|
-
description: data.notes,
|
|
3487
|
-
contactId: data.contactId,
|
|
3488
|
-
opportunityId: data.opportunityId,
|
|
3489
|
-
});
|
|
3490
|
-
}
|
|
3491
|
-
/**
|
|
3492
|
-
* Add a note to a contact or opportunity
|
|
3493
|
-
*/
|
|
3494
|
-
async addNote(data) {
|
|
3495
|
-
return this.createActivity({
|
|
3496
|
-
type: 'note',
|
|
3497
|
-
title: 'Note',
|
|
3498
|
-
description: data.content,
|
|
3499
|
-
contactId: data.contactId,
|
|
3500
|
-
opportunityId: data.opportunityId,
|
|
3501
|
-
});
|
|
3502
|
-
}
|
|
3503
|
-
// ============================================
|
|
3504
|
-
// EMAIL TEMPLATES API
|
|
3505
|
-
// ============================================
|
|
3506
|
-
/**
|
|
3507
|
-
* Get all email templates
|
|
3508
|
-
*/
|
|
3509
|
-
async getEmailTemplates(params) {
|
|
3510
|
-
const queryParams = new URLSearchParams();
|
|
3511
|
-
if (params?.page)
|
|
3512
|
-
queryParams.set('page', params.page.toString());
|
|
3513
|
-
if (params?.limit)
|
|
3514
|
-
queryParams.set('limit', params.limit.toString());
|
|
3515
|
-
const query = queryParams.toString();
|
|
3516
|
-
const endpoint = `/api/workspaces/${this.workspaceId}/email-templates${query ? `?${query}` : ''}`;
|
|
3517
|
-
return this.request(endpoint);
|
|
3518
|
-
}
|
|
3519
|
-
/**
|
|
3520
|
-
* Get a single email template by ID
|
|
3521
|
-
*/
|
|
3522
|
-
async getEmailTemplate(templateId) {
|
|
3523
|
-
return this.request(`/api/workspaces/${this.workspaceId}/email-templates/${templateId}`);
|
|
3524
|
-
}
|
|
3525
|
-
/**
|
|
3526
|
-
* Create a new email template
|
|
3527
|
-
*/
|
|
3528
|
-
async createEmailTemplate(template) {
|
|
3529
|
-
return this.request(`/api/workspaces/${this.workspaceId}/email-templates`, {
|
|
3530
|
-
method: 'POST',
|
|
3531
|
-
body: JSON.stringify(template),
|
|
3532
|
-
});
|
|
3533
|
-
}
|
|
3534
|
-
/**
|
|
3535
|
-
* Update an email template
|
|
3536
|
-
*/
|
|
3537
|
-
async updateEmailTemplate(templateId, updates) {
|
|
3538
|
-
return this.request(`/api/workspaces/${this.workspaceId}/email-templates/${templateId}`, {
|
|
3539
|
-
method: 'PUT',
|
|
3540
|
-
body: JSON.stringify(updates),
|
|
3541
|
-
});
|
|
3542
|
-
}
|
|
3543
|
-
/**
|
|
3544
|
-
* Delete an email template
|
|
3545
|
-
*/
|
|
3546
|
-
async deleteEmailTemplate(templateId) {
|
|
3547
|
-
return this.request(`/api/workspaces/${this.workspaceId}/email-templates/${templateId}`, {
|
|
3548
|
-
method: 'DELETE',
|
|
3549
|
-
});
|
|
3550
|
-
}
|
|
3551
|
-
/**
|
|
3552
|
-
* Send an email using a template
|
|
3553
|
-
*/
|
|
3554
|
-
async sendEmail(data) {
|
|
3555
|
-
return this.request(`/api/workspaces/${this.workspaceId}/emails/send`, {
|
|
3556
|
-
method: 'POST',
|
|
3557
|
-
body: JSON.stringify(data),
|
|
3558
|
-
});
|
|
3559
|
-
}
|
|
3560
|
-
// ============================================
|
|
3561
|
-
// READ-BACK / DATA RETRIEVAL API
|
|
3562
|
-
// ============================================
|
|
3563
|
-
/**
|
|
3564
|
-
* Get a contact by email address.
|
|
3565
|
-
* Returns the first matching contact from a search query.
|
|
3566
|
-
*/
|
|
3567
|
-
async getContactByEmail(email) {
|
|
3568
|
-
this.validateRequired('email', email, 'getContactByEmail');
|
|
3569
|
-
const queryParams = new URLSearchParams({ search: email, limit: '1' });
|
|
3570
|
-
return this.request(`/api/workspaces/${this.workspaceId}/contacts?${queryParams.toString()}`);
|
|
3571
|
-
}
|
|
3572
|
-
/**
|
|
3573
|
-
* Get activity timeline for a contact
|
|
3574
|
-
*/
|
|
3575
|
-
async getContactActivity(contactId, params) {
|
|
3576
|
-
this.validateRequired('contactId', contactId, 'getContactActivity');
|
|
3577
|
-
const queryParams = new URLSearchParams();
|
|
3578
|
-
if (params?.page)
|
|
3579
|
-
queryParams.set('page', params.page.toString());
|
|
3580
|
-
if (params?.limit)
|
|
3581
|
-
queryParams.set('limit', params.limit.toString());
|
|
3582
|
-
if (params?.type)
|
|
3583
|
-
queryParams.set('type', params.type);
|
|
3584
|
-
if (params?.startDate)
|
|
3585
|
-
queryParams.set('startDate', params.startDate);
|
|
3586
|
-
if (params?.endDate)
|
|
3587
|
-
queryParams.set('endDate', params.endDate);
|
|
3588
|
-
const query = queryParams.toString();
|
|
3589
|
-
const endpoint = `/api/workspaces/${this.workspaceId}/contacts/${contactId}/activities${query ? `?${query}` : ''}`;
|
|
3590
|
-
return this.request(endpoint);
|
|
3591
|
-
}
|
|
3592
|
-
/**
|
|
3593
|
-
* Get engagement metrics for a contact (via their linked visitor data)
|
|
3594
|
-
*/
|
|
3595
|
-
async getContactEngagement(contactId) {
|
|
3596
|
-
this.validateRequired('contactId', contactId, 'getContactEngagement');
|
|
3597
|
-
return this.request(`/api/workspaces/${this.workspaceId}/contacts/${contactId}/engagement`);
|
|
3598
|
-
}
|
|
3599
|
-
/**
|
|
3600
|
-
* Get a full timeline for a contact including events, activities, and opportunities
|
|
3601
|
-
*/
|
|
3602
|
-
async getContactTimeline(contactId, params) {
|
|
3603
|
-
this.validateRequired('contactId', contactId, 'getContactTimeline');
|
|
3604
|
-
const queryParams = new URLSearchParams();
|
|
3605
|
-
if (params?.page)
|
|
3606
|
-
queryParams.set('page', params.page.toString());
|
|
3607
|
-
if (params?.limit)
|
|
3608
|
-
queryParams.set('limit', params.limit.toString());
|
|
3609
|
-
const query = queryParams.toString();
|
|
3610
|
-
const endpoint = `/api/workspaces/${this.workspaceId}/contacts/${contactId}/timeline${query ? `?${query}` : ''}`;
|
|
3611
|
-
return this.request(endpoint);
|
|
3612
|
-
}
|
|
3613
|
-
/**
|
|
3614
|
-
* Search contacts with advanced filters
|
|
3615
|
-
*/
|
|
3616
|
-
async searchContacts(query, filters) {
|
|
3617
|
-
const queryParams = new URLSearchParams();
|
|
3618
|
-
queryParams.set('search', query);
|
|
3619
|
-
if (filters?.status)
|
|
3620
|
-
queryParams.set('status', filters.status);
|
|
3621
|
-
if (filters?.lifecycleStage)
|
|
3622
|
-
queryParams.set('lifecycleStage', filters.lifecycleStage);
|
|
3623
|
-
if (filters?.source)
|
|
3624
|
-
queryParams.set('source', filters.source);
|
|
3625
|
-
if (filters?.tags)
|
|
3626
|
-
queryParams.set('tags', filters.tags.join(','));
|
|
3627
|
-
if (filters?.page)
|
|
3628
|
-
queryParams.set('page', filters.page.toString());
|
|
3629
|
-
if (filters?.limit)
|
|
3630
|
-
queryParams.set('limit', filters.limit.toString());
|
|
3631
|
-
const qs = queryParams.toString();
|
|
3632
|
-
const endpoint = `/api/workspaces/${this.workspaceId}/contacts${qs ? `?${qs}` : ''}`;
|
|
3633
|
-
return this.request(endpoint);
|
|
3634
|
-
}
|
|
3635
|
-
// ============================================
|
|
3636
|
-
// WEBHOOK MANAGEMENT API
|
|
3637
|
-
// ============================================
|
|
3638
|
-
/**
|
|
3639
|
-
* List all webhook subscriptions
|
|
3640
|
-
*/
|
|
3641
|
-
async listWebhooks(params) {
|
|
3642
|
-
const queryParams = new URLSearchParams();
|
|
3643
|
-
if (params?.page)
|
|
3644
|
-
queryParams.set('page', params.page.toString());
|
|
3645
|
-
if (params?.limit)
|
|
3646
|
-
queryParams.set('limit', params.limit.toString());
|
|
3647
|
-
const query = queryParams.toString();
|
|
3648
|
-
return this.request(`/api/workspaces/${this.workspaceId}/webhooks${query ? `?${query}` : ''}`);
|
|
3649
|
-
}
|
|
3650
|
-
/**
|
|
3651
|
-
* Create a new webhook subscription
|
|
3652
|
-
*/
|
|
3653
|
-
async createWebhook(data) {
|
|
3654
|
-
return this.request(`/api/workspaces/${this.workspaceId}/webhooks`, {
|
|
3655
|
-
method: 'POST',
|
|
3656
|
-
body: JSON.stringify(data),
|
|
3657
|
-
});
|
|
3658
|
-
}
|
|
3659
|
-
/**
|
|
3660
|
-
* Delete a webhook subscription
|
|
3661
|
-
*/
|
|
3662
|
-
async deleteWebhook(webhookId) {
|
|
3663
|
-
this.validateRequired('webhookId', webhookId, 'deleteWebhook');
|
|
3664
|
-
return this.request(`/api/workspaces/${this.workspaceId}/webhooks/${webhookId}`, {
|
|
3665
|
-
method: 'DELETE',
|
|
3666
|
-
});
|
|
3667
|
-
}
|
|
3668
|
-
// ============================================
|
|
3669
|
-
// EVENT TRIGGERS API (delegated to triggers manager)
|
|
3670
|
-
// ============================================
|
|
3671
|
-
/**
|
|
3672
|
-
* Get all event triggers
|
|
3673
|
-
*/
|
|
3674
|
-
async getEventTriggers() {
|
|
3675
|
-
return this.triggers.getTriggers();
|
|
3676
|
-
}
|
|
3677
|
-
/**
|
|
3678
|
-
* Create a new event trigger
|
|
3679
|
-
*/
|
|
3680
|
-
async createEventTrigger(trigger) {
|
|
3681
|
-
return this.triggers.createTrigger(trigger);
|
|
3682
|
-
}
|
|
3683
|
-
/**
|
|
3684
|
-
* Update an event trigger
|
|
3685
|
-
*/
|
|
3686
|
-
async updateEventTrigger(triggerId, updates) {
|
|
3687
|
-
return this.triggers.updateTrigger(triggerId, updates);
|
|
3688
|
-
}
|
|
3689
|
-
/**
|
|
3690
|
-
* Delete an event trigger
|
|
3691
|
-
*/
|
|
3692
|
-
async deleteEventTrigger(triggerId) {
|
|
3693
|
-
return this.triggers.deleteTrigger(triggerId);
|
|
3694
|
-
}
|
|
3695
|
-
}
|
|
3696
|
-
|
|
3697
|
-
/**
|
|
3698
|
-
* Privacy-safe visitor API client.
|
|
3699
|
-
* All methods return data for the current visitor only (no cross-visitor access).
|
|
3700
|
-
*/
|
|
3701
|
-
class VisitorClient {
|
|
3702
|
-
constructor(transport, workspaceId, visitorId) {
|
|
3703
|
-
this.transport = transport;
|
|
3704
|
-
this.workspaceId = workspaceId;
|
|
3705
|
-
this.visitorId = visitorId;
|
|
3706
|
-
}
|
|
3707
|
-
/** Update visitorId (e.g. after reset) */
|
|
3708
|
-
setVisitorId(id) {
|
|
3709
|
-
this.visitorId = id;
|
|
3710
|
-
}
|
|
3711
|
-
basePath() {
|
|
3712
|
-
return `/api/public/track/visitor/${this.workspaceId}/${this.visitorId}`;
|
|
3713
|
-
}
|
|
3714
|
-
/**
|
|
3715
|
-
* Get the current visitor's profile from the CRM.
|
|
3716
|
-
* Returns visitor data and linked contact info if identified.
|
|
3717
|
-
*/
|
|
3718
|
-
async getProfile() {
|
|
3719
|
-
const result = await this.transport.fetchData(`${this.basePath()}/profile`);
|
|
3720
|
-
if (result.success && result.data) {
|
|
3721
|
-
logger.debug('Visitor profile fetched:', result.data);
|
|
3722
|
-
return result.data;
|
|
3723
|
-
}
|
|
3724
|
-
logger.warn('Failed to fetch visitor profile:', result.error);
|
|
3725
|
-
return null;
|
|
3726
|
-
}
|
|
3727
|
-
/**
|
|
3728
|
-
* Get the current visitor's recent activity/events.
|
|
3729
|
-
* Returns paginated list of tracking events.
|
|
3730
|
-
*/
|
|
3731
|
-
async getActivity(options) {
|
|
3732
|
-
const params = {};
|
|
3733
|
-
if (options?.page)
|
|
3734
|
-
params.page = options.page.toString();
|
|
3735
|
-
if (options?.limit)
|
|
3736
|
-
params.limit = options.limit.toString();
|
|
3737
|
-
if (options?.eventType)
|
|
3738
|
-
params.eventType = options.eventType;
|
|
3739
|
-
if (options?.startDate)
|
|
3740
|
-
params.startDate = options.startDate;
|
|
3741
|
-
if (options?.endDate)
|
|
3742
|
-
params.endDate = options.endDate;
|
|
3743
|
-
const result = await this.transport.fetchData(`${this.basePath()}/activity`, params);
|
|
3744
|
-
if (result.success && result.data) {
|
|
3745
|
-
return result.data;
|
|
3746
|
-
}
|
|
3747
|
-
logger.warn('Failed to fetch visitor activity:', result.error);
|
|
3748
|
-
return null;
|
|
3749
|
-
}
|
|
3750
|
-
/**
|
|
3751
|
-
* Get a summarized journey timeline for the current visitor.
|
|
3752
|
-
* Includes top pages, sessions, time spent, and recent activities.
|
|
3753
|
-
*/
|
|
3754
|
-
async getTimeline() {
|
|
3755
|
-
const result = await this.transport.fetchData(`${this.basePath()}/timeline`);
|
|
3756
|
-
if (result.success && result.data) {
|
|
3757
|
-
return result.data;
|
|
3758
|
-
}
|
|
3759
|
-
logger.warn('Failed to fetch visitor timeline:', result.error);
|
|
3760
|
-
return null;
|
|
3761
|
-
}
|
|
3762
|
-
/**
|
|
3763
|
-
* Get engagement metrics for the current visitor.
|
|
3764
|
-
* Includes time on site, page views, bounce rate, and engagement score.
|
|
3765
|
-
*/
|
|
3766
|
-
async getEngagement() {
|
|
3767
|
-
const result = await this.transport.fetchData(`${this.basePath()}/engagement`);
|
|
3768
|
-
if (result.success && result.data) {
|
|
3769
|
-
return result.data;
|
|
3770
|
-
}
|
|
3771
|
-
logger.warn('Failed to fetch visitor engagement:', result.error);
|
|
3772
|
-
return null;
|
|
3773
|
-
}
|
|
3774
|
-
}
|
|
3775
|
-
|
|
3776
2514
|
/**
|
|
3777
2515
|
* Clianta SDK - Main Tracker Class
|
|
3778
2516
|
* @see SDK_VERSION in core/config.ts
|
|
@@ -3821,17 +2559,12 @@ class Tracker {
|
|
|
3821
2559
|
this.visitorId = this.createVisitorId();
|
|
3822
2560
|
this.sessionId = this.createSessionId();
|
|
3823
2561
|
logger.debug('IDs created', { visitorId: this.visitorId, sessionId: this.sessionId });
|
|
3824
|
-
// Initialize visitor API client
|
|
3825
|
-
this.visitor = new VisitorClient(this.transport, this.workspaceId, this.visitorId);
|
|
3826
2562
|
// Security warnings
|
|
3827
2563
|
if (this.config.apiEndpoint.startsWith('http://') &&
|
|
3828
2564
|
typeof window !== 'undefined' &&
|
|
3829
2565
|
!window.location.hostname.includes('localhost') &&
|
|
3830
2566
|
!window.location.hostname.includes('127.0.0.1')) {
|
|
3831
|
-
logger.warn('apiEndpoint uses HTTP — events
|
|
3832
|
-
}
|
|
3833
|
-
if (this.config.apiKey && typeof window !== 'undefined') {
|
|
3834
|
-
logger.warn('API key is exposed in client-side code. Use API keys only in server-side (Node.js) environments.');
|
|
2567
|
+
logger.warn('apiEndpoint uses HTTP — events will be sent unencrypted. Use HTTPS in production.');
|
|
3835
2568
|
}
|
|
3836
2569
|
// Initialize plugins
|
|
3837
2570
|
this.initPlugins();
|
|
@@ -4027,63 +2760,6 @@ class Tracker {
|
|
|
4027
2760
|
return null;
|
|
4028
2761
|
}
|
|
4029
2762
|
}
|
|
4030
|
-
/**
|
|
4031
|
-
* Send a server-side inbound event via the API key endpoint.
|
|
4032
|
-
* Convenience proxy to CRMClient.sendEvent() — requires apiKey in config.
|
|
4033
|
-
*/
|
|
4034
|
-
async sendEvent(payload) {
|
|
4035
|
-
const apiKey = this.config.apiKey;
|
|
4036
|
-
if (!apiKey) {
|
|
4037
|
-
logger.error('sendEvent() requires an apiKey in the SDK config');
|
|
4038
|
-
return { success: false, contactCreated: false, event: payload.event, error: 'No API key configured' };
|
|
4039
|
-
}
|
|
4040
|
-
const client = new CRMClient(this.config.apiEndpoint, this.workspaceId, undefined, apiKey);
|
|
4041
|
-
return client.sendEvent(payload);
|
|
4042
|
-
}
|
|
4043
|
-
/**
|
|
4044
|
-
* Get the current visitor's profile from the CRM.
|
|
4045
|
-
* @deprecated Use `tracker.visitor.getProfile()` instead.
|
|
4046
|
-
*/
|
|
4047
|
-
async getVisitorProfile() {
|
|
4048
|
-
if (!this.isInitialized) {
|
|
4049
|
-
logger.warn('SDK not initialized');
|
|
4050
|
-
return null;
|
|
4051
|
-
}
|
|
4052
|
-
return this.visitor.getProfile();
|
|
4053
|
-
}
|
|
4054
|
-
/**
|
|
4055
|
-
* Get the current visitor's recent activity/events.
|
|
4056
|
-
* @deprecated Use `tracker.visitor.getActivity()` instead.
|
|
4057
|
-
*/
|
|
4058
|
-
async getVisitorActivity(options) {
|
|
4059
|
-
if (!this.isInitialized) {
|
|
4060
|
-
logger.warn('SDK not initialized');
|
|
4061
|
-
return null;
|
|
4062
|
-
}
|
|
4063
|
-
return this.visitor.getActivity(options);
|
|
4064
|
-
}
|
|
4065
|
-
/**
|
|
4066
|
-
* Get a summarized journey timeline for the current visitor.
|
|
4067
|
-
* @deprecated Use `tracker.visitor.getTimeline()` instead.
|
|
4068
|
-
*/
|
|
4069
|
-
async getVisitorTimeline() {
|
|
4070
|
-
if (!this.isInitialized) {
|
|
4071
|
-
logger.warn('SDK not initialized');
|
|
4072
|
-
return null;
|
|
4073
|
-
}
|
|
4074
|
-
return this.visitor.getTimeline();
|
|
4075
|
-
}
|
|
4076
|
-
/**
|
|
4077
|
-
* Get engagement metrics for the current visitor.
|
|
4078
|
-
* @deprecated Use `tracker.visitor.getEngagement()` instead.
|
|
4079
|
-
*/
|
|
4080
|
-
async getVisitorEngagement() {
|
|
4081
|
-
if (!this.isInitialized) {
|
|
4082
|
-
logger.warn('SDK not initialized');
|
|
4083
|
-
return null;
|
|
4084
|
-
}
|
|
4085
|
-
return this.visitor.getEngagement();
|
|
4086
|
-
}
|
|
4087
2763
|
/**
|
|
4088
2764
|
* Retry pending identify call
|
|
4089
2765
|
*/
|
|
@@ -4493,7 +3169,12 @@ class Tracker {
|
|
|
4493
3169
|
|
|
4494
3170
|
/**
|
|
4495
3171
|
* Clianta SDK
|
|
4496
|
-
*
|
|
3172
|
+
* Client-side tracking SDK for CRM — tracks visitors, identifies contacts,
|
|
3173
|
+
* captures forms, and writes CRM data from client websites.
|
|
3174
|
+
*
|
|
3175
|
+
* This SDK is designed to run on CLIENT WEBSITES (React, Next.js, Vue, etc.)
|
|
3176
|
+
* It only SENDS data to your CRM — it never reads CRM data back.
|
|
3177
|
+
*
|
|
4497
3178
|
* @see SDK_VERSION in core/config.ts
|
|
4498
3179
|
*/
|
|
4499
3180
|
// Global instance cache
|
|
@@ -4535,21 +3216,43 @@ function clianta(workspaceId, config) {
|
|
|
4535
3216
|
globalInstance = new Tracker(workspaceId, config);
|
|
4536
3217
|
return globalInstance;
|
|
4537
3218
|
}
|
|
4538
|
-
// Attach to window for <script> usage
|
|
3219
|
+
// Attach to window for <script> tag usage + AUTO-INIT
|
|
4539
3220
|
if (typeof window !== 'undefined') {
|
|
4540
3221
|
window.clianta = clianta;
|
|
4541
3222
|
window.Clianta = {
|
|
4542
3223
|
clianta,
|
|
4543
3224
|
Tracker,
|
|
4544
|
-
CRMClient,
|
|
4545
3225
|
ConsentManager,
|
|
4546
|
-
EventTriggersManager,
|
|
4547
3226
|
};
|
|
3227
|
+
// ============================================
|
|
3228
|
+
// AUTO-INIT FROM SCRIPT TAG
|
|
3229
|
+
// ============================================
|
|
3230
|
+
// Enables true plug-and-play:
|
|
3231
|
+
// <script src="clianta.min.js" data-project-id="YOUR_ID"></script>
|
|
3232
|
+
// That's it — everything auto-tracks.
|
|
3233
|
+
const autoInit = () => {
|
|
3234
|
+
const scripts = document.querySelectorAll('script[data-project-id]');
|
|
3235
|
+
const script = scripts[scripts.length - 1]; // last matching script
|
|
3236
|
+
if (!script)
|
|
3237
|
+
return;
|
|
3238
|
+
const projectId = script.getAttribute('data-project-id');
|
|
3239
|
+
if (!projectId)
|
|
3240
|
+
return;
|
|
3241
|
+
const debug = script.hasAttribute('data-debug');
|
|
3242
|
+
const instance = clianta(projectId, { debug });
|
|
3243
|
+
// Expose the auto-initialized instance globally
|
|
3244
|
+
window.__clianta = instance;
|
|
3245
|
+
};
|
|
3246
|
+
// Run after DOM is ready
|
|
3247
|
+
if (document.readyState === 'loading') {
|
|
3248
|
+
document.addEventListener('DOMContentLoaded', autoInit);
|
|
3249
|
+
}
|
|
3250
|
+
else {
|
|
3251
|
+
autoInit();
|
|
3252
|
+
}
|
|
4548
3253
|
}
|
|
4549
3254
|
|
|
4550
|
-
exports.CRMClient = CRMClient;
|
|
4551
3255
|
exports.ConsentManager = ConsentManager;
|
|
4552
|
-
exports.EventTriggersManager = EventTriggersManager;
|
|
4553
3256
|
exports.SDK_VERSION = SDK_VERSION;
|
|
4554
3257
|
exports.Tracker = Tracker;
|
|
4555
3258
|
exports.clianta = clianta;
|