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