/** * Copyright 2024-2026 Wingify Software Pvt. Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { getEventsBaseProperties, getSDKInitEventPayload, getSDKUsageStatsEventPayload, sendEvent, } from './NetworkUtil'; import { EventEnum } from '../enums/EventEnum'; import { ServiceContainer } from '../services/ServiceContainer'; import { UsageStatsUtil } from './UsageStatsUtil'; /** * Sends an init called event to Wingify. * This event is triggered when the init function is called. * @param {number} settingsFetchTime - Time taken to fetch settings in milliseconds. * @param {number} sdkInitTime - Time taken to initialize the SDK in milliseconds. * @param {ServiceContainer} serviceContainer - The service container instance. */ export async function sendSdkInitEvent( settingsFetchTime: number, sdkInitTime: number, serviceContainer: ServiceContainer, ) { // create the query parameters const properties = getEventsBaseProperties(serviceContainer.getSettingsService(), EventEnum.INIT_CALLED); // create the payload with required fields const payload = getSDKInitEventPayload( serviceContainer.getSettingsService(), EventEnum.INIT_CALLED, settingsFetchTime, sdkInitTime, ); if (serviceContainer.getBatchEventsQueue()) { serviceContainer.getBatchEventsQueue().enqueue(payload); } else { // Send the constructed properties and payload as a POST request //send eventName in parameters so that we can enable retry for this event await sendEvent(serviceContainer, properties, payload, EventEnum.INIT_CALLED).catch(() => {}); } } /** * Sends a usage stats event to Wingify. * This event is triggered when the SDK is initialized. * @returns A promise that resolves to the response from the server. */ export async function sendSDKUsageStatsEvent( usageStatsAccountId: number, serviceContainer: ServiceContainer, usageStatsUtil: UsageStatsUtil, ) { // create the query parameters const properties = getEventsBaseProperties( serviceContainer.getSettingsService(), EventEnum.USAGE_STATS, null, null, true, usageStatsAccountId, ); // create the payload with required fields const payload = getSDKUsageStatsEventPayload( serviceContainer.getSettingsService(), EventEnum.USAGE_STATS, usageStatsAccountId, usageStatsUtil, ); if (serviceContainer.getBatchEventsQueue()) { serviceContainer.getBatchEventsQueue().enqueue(payload); } else { // Send the constructed properties and payload as a POST request //send eventName in parameters so that we can enable retry for this event await sendEvent(serviceContainer, properties, payload, EventEnum.USAGE_STATS).catch(() => {}); } }