tianji-client-sdk
Version:
111 lines (110 loc) • 3.46 kB
JavaScript
;
/**
* 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;
let options;
function initApplication(_options) {
options = _options;
}
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;
}
const payload = {
application: options.applicationId,
screen: screenName ?? currentScreenName,
params: screenParams ?? currentScreenParams,
};
sendApplicationRequest(options.serverUrl, '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,
};
sendApplicationRequest(options.serverUrl, '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,
};
sendApplicationRequest(options.serverUrl, 'identify', payload);
}
/**
* Send request to Tianji application API
*
* @param serverUrl - Tianji server URL
* @param type - Request type ('event' or 'identify')
* @param payload - Request payload
* @returns Promise with token string
*/
async function sendApplicationRequest(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
}
}