@snap/camera-kit
Version:
Camera Kit Web
156 lines • 7.76 kB
JavaScript
import { __awaiter } from "tslib";
import { v4 } from "uuid";
import { catchError, combineLatestWith, from, fromEvent, map, merge, of, switchMap, take } from "rxjs";
import { entries } from "../common/entries";
import { Injectable } from "../dependency-injection/Injectable";
import * as blizzard from "../generated-proto/blizzard/cameraKitEvents";
import { getLogger } from "../logger/logger";
import { metricsClientFactory } from "../clients/metricsClient";
import { configurationToken } from "../configuration";
import { remoteConfigurationFactory } from "../remote-configuration/remoteConfiguration";
import { IndexedDBPersistence } from "../persistence/IndexedDBPersistence";
import { ExpiringPersistence } from "../persistence/ExpiringPersistence";
import { convertDaysToSeconds } from "../common/time";
import { getPlatformInfo } from "../platform/platformInfo";
import { metricsEventTargetFactory } from "./metricsEventTarget";
const logger = getLogger("BusinessEventsReporter");
const connectivityTypeMapping = {
cellular: blizzard.CameraKitConnectivityType.CAMERA_KIT_CONNECTIVITY_TYPE_MOBILE,
bluetooth: blizzard.CameraKitConnectivityType.CAMERA_KIT_CONNECTIVITY_TYPE_BLUETOOTH,
wifi: blizzard.CameraKitConnectivityType.CAMERA_KIT_CONNECTIVITY_TYPE_WIFI,
unknown: blizzard.CameraKitConnectivityType.CAMERA_KIT_CONNECTIVITY_TYPE_UNKNOWN,
none: blizzard.CameraKitConnectivityType.CAMERA_KIT_CONNECTIVITY_TYPE_UNREACHABLE,
};
const vendorUuidKey = "vendorUuid";
const vendorUuidExpiry = convertDaysToSeconds(60);
const getOrGenerateVendorUuid = (persistence) => __awaiter(void 0, void 0, void 0, function* () {
try {
const storedUuid = yield persistence.retrieve(vendorUuidKey);
if (storedUuid) {
return storedUuid;
}
const newUuid = v4();
yield persistence.store(vendorUuidKey, newUuid);
return newUuid;
}
catch (error) {
throw new Error("Failed to generate vendor UUID");
}
});
function listenAndReport(metricsEventTarget, metricsClient, eventHandlers, appVendorAndPartnerUuid) {
const sessionId = v4();
logger.log(`Session ID: ${sessionId}`);
let sequenceId = 1;
const makeBlizzardEvent = (event, appVendorUuid, partnerUuid) => {
var _a;
const { sdkShortVersion, sdkLongVersion, lensCore, locale, origin, deviceModel, connectionType } = getPlatformInfo();
const deviceConnectivity = (_a = connectivityTypeMapping[connectionType]) !== null && _a !== void 0 ? _a : blizzard.CameraKitConnectivityType.CAMERA_KIT_CONNECTIVITY_TYPE_UNKNOWN;
return Object.assign(Object.assign({}, event), { cameraKitEventBase: blizzard.CameraKitEventBase.fromPartial({
kitEventBase: blizzard.KitEventBase.fromPartial({
locale,
kitVariant: blizzard.KitType.CAMERA_KIT_WEB,
kitVariantVersion: sdkShortVersion,
kitClientTimestampMillis: `${Date.now()}`,
}),
deviceCluster: "0",
cameraKitVersion: sdkLongVersion,
lensCoreVersion: lensCore.version,
deviceModel,
cameraKitVariant: blizzard.CameraKitVariant.CAMERA_KIT_VARIANT_PARTNER,
cameraKitFlavor: blizzard.CameraKitFlavor.CAMERA_KIT_FLAVOR_DEBUG,
appId: origin,
deviceConnectivity,
sessionId,
appVendorUuid,
partnerUuid,
}) });
};
const sendServerEvent = (eventName, eventData) => {
const { osName: osType, osVersion } = getPlatformInfo();
return metricsClient.setBusinessEvents(blizzard.ServerEvent.fromPartial({
eventName,
osType,
osVersion,
maxSequenceIdOnInstance: "0",
sequenceId: `${sequenceId++}`,
eventData,
}));
};
const metricsEvents = entries(eventHandlers).map(([eventType, createEventData]) => fromEvent(metricsEventTarget, eventType).pipe(map((event) => ({ event, createEventData }))));
merge(...metricsEvents)
.pipe(combineLatestWith(appVendorAndPartnerUuid))
.subscribe(([{ event, createEventData }, { appVendorUuid, partnerUuid }]) => {
const [eventName, eventData] = createEventData(makeBlizzardEvent(event.detail, appVendorUuid, partnerUuid));
sendServerEvent(eventName, eventData);
});
}
function getAppVendorAndPartnerUuid(configuration, remoteConfiguration) {
const vendorAnalyticsPersistence = new ExpiringPersistence(() => vendorUuidExpiry, new IndexedDBPersistence({ databaseName: "VendorAnalytics" }));
return remoteConfiguration.getInitializationConfig().pipe(take(1), switchMap(({ appVendorUuidOptIn }) => {
const partnerUuid = configuration.analyticsId;
if (appVendorUuidOptIn) {
return from(getOrGenerateVendorUuid(vendorAnalyticsPersistence)).pipe(map((appVendorUuid) => ({ appVendorUuid, partnerUuid })));
}
return of({ appVendorUuid: undefined, partnerUuid });
}), catchError((error) => {
logger.warn(`Failed to retrieve or generate vendor UUID.`, error);
return of({ appVendorUuid: undefined, partnerUuid: configuration.analyticsId });
}));
}
export const businessEventsReporterFactory = Injectable("businessEventsReporter", [
metricsEventTargetFactory.token,
metricsClientFactory.token,
configurationToken,
remoteConfigurationFactory.token,
], (metricsEventTarget, metricsClient, configuration, remoteConfiguration) => {
const appVendorAndPartnerUuid = getAppVendorAndPartnerUuid(configuration, remoteConfiguration);
listenAndReport(metricsEventTarget, metricsClient, {
assetDownload: (event) => [
"CAMERA_KIT_ASSET_DOWNLOAD",
{ cameraKitAssetDownload: blizzard.CameraKitAssetDownload.fromPartial(event) },
],
assetValidationFailed: (event) => [
"CAMERA_KIT_ASSET_VALIDATION_FAILED",
{
cameraKitAssetValidationFailed: blizzard.CameraKitAssetValidationFailed.fromPartial(event),
},
],
benchmarkComplete: (event) => [
"CAMERA_KIT_WEB_BENCHMARK_COMPLETE",
{
cameraKitWebBenchmarkComplete: blizzard.CameraKitWebBenchmarkComplete.fromPartial(event),
},
],
exception: (event) => [
"CAMERA_KIT_EXCEPTION",
{ cameraKitException: blizzard.CameraKitException.fromPartial(event) },
],
legalPrompt: (event) => [
"CAMERA_KIT_LEGAL_PROMPT",
{ cameraKitLegalPrompt: blizzard.CameraKitLegalPrompt.fromPartial(event) },
],
lensDownload: (event) => [
"CAMERA_KIT_LENS_DOWNLOAD",
{ cameraKitLensDownload: blizzard.CameraKitLensDownload.fromPartial(event) },
],
lensView: (event) => [
"CAMERA_KIT_WEB_LENS_SWIPE",
{ cameraKitWebLensSwipe: blizzard.CameraKitWebLensSwipe.fromPartial(event) },
],
lensWait: (event) => [
"CAMERA_KIT_LENS_SPIN",
{ cameraKitLensSpin: blizzard.CameraKitLensSpin.fromPartial(event) },
],
lensContentValidationFailed: (event) => [
"CAMERA_KIT_LENS_CONTENT_VALIDATION_FAILED",
{
cameraKitLensContentValidationFailed: blizzard.CameraKitLensContentValidationFailed.fromPartial(event),
},
],
session: (event) => [
"CAMERA_KIT_SESSION",
{ cameraKitSession: blizzard.CameraKitSession.fromPartial(event) },
],
}, appVendorAndPartnerUuid);
});
//# sourceMappingURL=businessEventsReporter.js.map