|
| 1 | +/** |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance |
| 5 | + * with the License. A copy of the License is located at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * or in the 'license' file accompanying this file. This file is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES |
| 10 | + * OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions |
| 11 | + * and limitations under the License. |
| 12 | + */ |
| 13 | +import { v4 as uuidV4 } from 'uuid'; |
| 14 | +import { ClickstreamContext } from './ClickstreamContext'; |
| 15 | +import { Event } from './Event'; |
| 16 | +import { |
| 17 | + AnalyticsEvent, |
| 18 | + ClickstreamAttribute, |
| 19 | + ClickstreamEvent, |
| 20 | + UserAttribute, |
| 21 | +} from '../types'; |
| 22 | +import { HashUtil } from '../util/HashUtil'; |
| 23 | +import { StorageUtil } from '../util/StorageUtil'; |
| 24 | + |
| 25 | +export class AnalyticsEventBuilder { |
| 26 | + static async createEvent( |
| 27 | + event: ClickstreamEvent, |
| 28 | + userAttributes: UserAttribute, |
| 29 | + clickstream: ClickstreamContext |
| 30 | + ): Promise<AnalyticsEvent> { |
| 31 | + const { browserInfo, configuration } = clickstream; |
| 32 | + const attributes = this.getEventAttributesWithCheck(event.attributes); |
| 33 | + const analyticEvent = { |
| 34 | + hashCode: '', |
| 35 | + event_type: event.name, |
| 36 | + event_id: uuidV4(), |
| 37 | + device_id: StorageUtil.getDeviceId(), |
| 38 | + unique_id: clickstream.userUniqueId, |
| 39 | + app_id: configuration.appId, |
| 40 | + timestamp: new Date().getTime(), |
| 41 | + host_name: browserInfo.hostName, |
| 42 | + locale: browserInfo.locale, |
| 43 | + system_language: browserInfo.system_language, |
| 44 | + country_code: browserInfo.country_code, |
| 45 | + zone_offset: browserInfo.zoneOffset, |
| 46 | + make: browserInfo.make, |
| 47 | + platform: 'Web', |
| 48 | + screen_height: window.innerHeight, |
| 49 | + screen_width: window.innerWidth, |
| 50 | + sdk_name: 'aws-solution-clickstream-sdk', |
| 51 | + sdk_version: process.env.VERSION ?? '', |
| 52 | + user: userAttributes ?? {}, |
| 53 | + attributes: attributes ?? {}, |
| 54 | + }; |
| 55 | + analyticEvent.hashCode = await HashUtil.getHashCode( |
| 56 | + JSON.stringify(analyticEvent) |
| 57 | + ); |
| 58 | + return analyticEvent; |
| 59 | + } |
| 60 | + |
| 61 | + static getEventAttributesWithCheck( |
| 62 | + attributes: ClickstreamAttribute |
| 63 | + ): ClickstreamAttribute { |
| 64 | + const resultAttributes: ClickstreamAttribute = {}; |
| 65 | + for (const key in attributes) { |
| 66 | + const value = attributes[key]; |
| 67 | + if (value !== null) { |
| 68 | + const currentNumber = Object.keys(resultAttributes).length; |
| 69 | + const { checkAttributes } = Event; |
| 70 | + const result = checkAttributes(currentNumber, key, value); |
| 71 | + const { ERROR_CODE, ERROR_MESSAGE } = Event.ReservedAttribute; |
| 72 | + if (result.error_code > 0 && !(ERROR_CODE in resultAttributes)) { |
| 73 | + resultAttributes[ERROR_CODE] = result.error_code; |
| 74 | + resultAttributes[ERROR_MESSAGE] = result.error_message; |
| 75 | + } else { |
| 76 | + resultAttributes[key] = value; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + return resultAttributes; |
| 81 | + } |
| 82 | +} |
0 commit comments