UNPKG

tianji-client-sdk

Version:

169 lines (168 loc) 5.17 kB
"use strict"; /** * Client SDK for Tianji Application API */ Object.defineProperty(exports, "__esModule", { value: true }); exports.initApplication = initApplication; exports.updateCurrentApplicationScreen = updateCurrentApplicationScreen; exports.reportApplicationScreenView = reportApplicationScreenView; exports.reportApplicationEvent = reportApplicationEvent; exports.identifyApplicationUser = identifyApplicationUser; exports.flushApplicationBatchQueue = flushApplicationBatchQueue; const batch_manager_1 = require("./utils/batch-manager"); let options; let batchManager = null; function initApplication(_options) { options = _options; // Initialize batch manager batchManager = new batch_manager_1.BatchManager({ batchDelay: _options.batchDelay, disableBatch: _options.disableBatch, onBatchSend: async (items) => { await sendBatchRequest(_options.serverUrl, items); }, onSingleSend: async (item) => { await sendSingleRequest(_options.serverUrl, item.type, item.payload); }, }); } let currentScreenName = undefined; let currentScreenParams = undefined; /** * Update current application screen */ function updateCurrentApplicationScreen(name, params) { currentScreenName = name; currentScreenParams = params; } /** * Send application screen view event to Tianji server */ async function reportApplicationScreenView(screenName, screenParams) { if (!options) { console.warn('Application tracking is not initialized'); return; } if (screenName) { currentScreenName = screenName; } if (screenParams) { currentScreenParams = screenParams; } const payload = { application: options.applicationId, screen: currentScreenName, params: currentScreenParams, }; sendRequest('event', payload); } /** * Send application event to Tianji server * * @param options - Application tracking options * @param eventName - Name of the event * @param eventData - Event data */ async function reportApplicationEvent(eventName, eventData = {}, screenName, screenParams) { if (!options) { console.warn('Application tracking is not initialized'); return; } const payload = { application: options.applicationId, name: eventName, data: eventData, screen: screenName ?? currentScreenName, params: screenParams ?? currentScreenParams, }; sendRequest('event', payload); } /** * Identify user in application * * @param options - Application tracking options * @param userData - User identification data */ async function identifyApplicationUser(userInfo) { if (!options) { console.warn('Application tracking is not initialized'); return; } if (!userInfo || Object.keys(userInfo).length === 0) { console.warn('User data is required for identification'); return; } const payload = { application: options.applicationId, data: userInfo, }; sendRequest('identify', payload); } /** * Send request with automatic batching */ function sendRequest(type, payload) { if (!batchManager) { console.warn('Application tracking is not initialized'); return; } batchManager.add(type, payload); } /** * Send single request to Tianji application API (fallback when batch is disabled) */ async function sendSingleRequest(serverUrl, type, payload) { try { const response = await fetch(`${serverUrl}/api/application/send`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ type, payload, }), }); if (!response.ok) { const errorText = await response.text(); throw new Error(`Failed to send application ${type}: ${errorText}`); } } catch (error) { console.error(`Error sending application ${type}:`, error); return; // As event tracking SDK, should not throw error which maybe cause crash } } /** * Send batch request to Tianji application API */ async function sendBatchRequest(serverUrl, items) { try { const response = await fetch(`${serverUrl}/api/application/batch`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ events: items, }), }); if (!response.ok) { const errorText = await response.text(); throw new Error(`Failed to send batch request: ${errorText}`); } } catch (error) { console.error('Error sending batch request:', error); return; // As event tracking SDK, should not throw error which maybe cause crash } } /** * Flush batch queue manually (send all pending requests immediately) * Useful for ensuring events are sent before app closes */ async function flushApplicationBatchQueue() { if (batchManager) { await batchManager.flush(); } }