|
| 1 | +import { |
| 2 | + NAME, |
| 3 | + DISPLAY_NAME, |
| 4 | +} from '@rudderstack/analytics-js-common/constants/integrations/GA4_V2/constants'; |
| 5 | +import { isDefinedAndNotNull } from '../../utils/commonUtils'; |
| 6 | +import Logger from '../../utils/logger'; |
| 7 | +import { GA4 } from '../GA4'; |
| 8 | + |
| 9 | +const logger = new Logger(DISPLAY_NAME); |
| 10 | + |
| 11 | +// eslint-disable-next-line @typescript-eslint/naming-convention |
| 12 | +export default class GA4_V2 extends GA4 { |
| 13 | + constructor(config, analytics, destinationInfo) { |
| 14 | + if (analytics.logLevel) { |
| 15 | + logger.setLogLevel(analytics.logLevel); |
| 16 | + } |
| 17 | + |
| 18 | + const newConfig = { ...config }; |
| 19 | + /** |
| 20 | + * Sample configData |
| 21 | + * "{\"ACCOUNT\":\"321500532\",\"PROPERTY\":\"449999397\",\"DATA_STREAM\":{\"value\":\"G-Q3C88MMDW1\",\"type\":\"gtag\"},\"MEASUREMENT_PROTOCOL_SECRET\":\"sample_secret\"}"; |
| 22 | + */ |
| 23 | + |
| 24 | + if (isDefinedAndNotNull(config.configData)) { |
| 25 | + const configDetails = JSON.parse(config.configData); |
| 26 | + newConfig.propertyId = configDetails.PROPERTY; |
| 27 | + newConfig.typesOfClient = configDetails.DATA_STREAM.type; |
| 28 | + if (newConfig.typesOfClient !== 'gtag') { |
| 29 | + logger.error('Invalid typesOfClient. Please select gtag'); |
| 30 | + } |
| 31 | + newConfig.measurementId = configDetails.DATA_STREAM.value; |
| 32 | + } |
| 33 | + |
| 34 | + if (!isDefinedAndNotNull(newConfig.measurementId)) { |
| 35 | + logger.error('Measurement ID is required for GA4'); |
| 36 | + } |
| 37 | + |
| 38 | + newConfig.isExtendedGa4_V2 = true; |
| 39 | + super(newConfig, analytics, destinationInfo); |
| 40 | + this.analytics = analytics; |
| 41 | + this.name = NAME; |
| 42 | + ({ |
| 43 | + shouldApplyDeviceModeTransformation: this.shouldApplyDeviceModeTransformation, |
| 44 | + propagateEventsUntransformedOnError: this.propagateEventsUntransformedOnError, |
| 45 | + destinationId: this.destinationId, |
| 46 | + } = destinationInfo ?? {}); |
| 47 | + } |
| 48 | + |
| 49 | + init() { |
| 50 | + return super.init(); |
| 51 | + } |
| 52 | + |
| 53 | + isLoaded() { |
| 54 | + return super.isLoaded(); |
| 55 | + } |
| 56 | + |
| 57 | + isReady() { |
| 58 | + return super.isReady(); |
| 59 | + } |
| 60 | + |
| 61 | + identify(rudderElement) { |
| 62 | + return super.identify(rudderElement); |
| 63 | + } |
| 64 | + |
| 65 | + track(rudderElement) { |
| 66 | + return super.track(rudderElement); |
| 67 | + } |
| 68 | + |
| 69 | + page(rudderElement) { |
| 70 | + return super.page(rudderElement); |
| 71 | + } |
| 72 | + |
| 73 | + group(rudderElement) { |
| 74 | + return super.group(rudderElement); |
| 75 | + } |
| 76 | + |
| 77 | + getDataForIntegrationsObject() { |
| 78 | + const { clientId, sessionId, sessionNumber } = super.getClientDetails(); |
| 79 | + return { |
| 80 | + [DISPLAY_NAME]: { |
| 81 | + clientId, |
| 82 | + sessionId, |
| 83 | + sessionNumber, |
| 84 | + }, |
| 85 | + }; |
| 86 | + } |
| 87 | +} |
0 commit comments