@reservamos/browser-analytics 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser-analytics.cjs +1 -1
- package/dist/browser-analytics.cjs.map +1 -1
- package/dist/browser-analytics.esm.js +33 -1
- package/dist/browser-analytics.esm.js.map +1 -1
- package/dist/browser-analytics.iife.js +1 -1
- package/dist/browser-analytics.iife.js.map +1 -1
- package/package.json +1 -1
- package/src/profiles/createAnonymousProfile/createAnonymousProfile.ts +20 -11
- package/src/track.ts +32 -0
package/package.json
CHANGED
|
@@ -10,7 +10,8 @@ type IdentifierKey = 'phone' | 'email';
|
|
|
10
10
|
interface AnonymousProfilePayload {
|
|
11
11
|
identifier_key: IdentifierKey;
|
|
12
12
|
identifier_value: string;
|
|
13
|
-
identifiers: AnonymousIdentifier[]
|
|
13
|
+
identifiers: AnonymousIdentifier[];
|
|
14
|
+
details: Record<string, string>;
|
|
14
15
|
}
|
|
15
16
|
interface AnonymousProfileResponse {
|
|
16
17
|
id: string;
|
|
@@ -23,11 +24,11 @@ interface AnonymousProfileResponse {
|
|
|
23
24
|
*/
|
|
24
25
|
function getAnonymousProfilePayload(
|
|
25
26
|
values: CreateAnonymousProfileProps,
|
|
26
|
-
identifiersProps: AnonymousIdentifier[]
|
|
27
|
+
identifiersProps: AnonymousIdentifier[],
|
|
27
28
|
): AnonymousProfilePayload {
|
|
28
29
|
let identifier_key: IdentifierKey = 'phone';
|
|
29
30
|
let identifier_value: string = values.phone || '';
|
|
30
|
-
const identifiers: AnonymousIdentifier[] = []
|
|
31
|
+
const identifiers: AnonymousIdentifier[] = [];
|
|
31
32
|
|
|
32
33
|
if (values.email) {
|
|
33
34
|
identifier_key = 'email';
|
|
@@ -35,7 +36,14 @@ function getAnonymousProfilePayload(
|
|
|
35
36
|
}
|
|
36
37
|
|
|
37
38
|
// List of allowed keys for details
|
|
38
|
-
const allowedKeys = [
|
|
39
|
+
const allowedKeys = [
|
|
40
|
+
'cpf',
|
|
41
|
+
'passport',
|
|
42
|
+
'rg',
|
|
43
|
+
'email',
|
|
44
|
+
'phone',
|
|
45
|
+
'salesforceid',
|
|
46
|
+
];
|
|
39
47
|
|
|
40
48
|
Object.entries(values).forEach(([key, value]) => {
|
|
41
49
|
if (allowedKeys.includes(key.toLowerCase()) && value) {
|
|
@@ -45,13 +53,14 @@ function getAnonymousProfilePayload(
|
|
|
45
53
|
}
|
|
46
54
|
});
|
|
47
55
|
|
|
48
|
-
if(identifiersProps.length){
|
|
49
|
-
|
|
56
|
+
if (identifiersProps.length) {
|
|
57
|
+
identifiersProps.forEach((item) => identifiers.push(item));
|
|
50
58
|
}
|
|
51
59
|
|
|
52
60
|
return {
|
|
53
61
|
identifier_key,
|
|
54
62
|
identifier_value,
|
|
63
|
+
details: {},
|
|
55
64
|
identifiers,
|
|
56
65
|
};
|
|
57
66
|
}
|
|
@@ -68,7 +77,7 @@ interface AnonymousIdentifier {
|
|
|
68
77
|
*/
|
|
69
78
|
async function createAnonymousProfile(
|
|
70
79
|
payload: CreateAnonymousProfileProps,
|
|
71
|
-
): Promise<AnonymousProfileResponse | undefined
|
|
80
|
+
): Promise<AnonymousProfileResponse | undefined> {
|
|
72
81
|
try {
|
|
73
82
|
validatorService.validateProps(payload, CreateAnonymousProfileSchema);
|
|
74
83
|
|
|
@@ -80,13 +89,13 @@ async function createAnonymousProfile(
|
|
|
80
89
|
identifiers.push({ key: 'fingerprint', value: userFingerprintId });
|
|
81
90
|
if (distinctId) identifiers.push({ key: 'distinct_id', value: distinctId });
|
|
82
91
|
|
|
83
|
-
const dataPayload = getAnonymousProfilePayload(payload,identifiers);
|
|
84
|
-
|
|
92
|
+
const dataPayload = getAnonymousProfilePayload(payload, identifiers);
|
|
93
|
+
|
|
85
94
|
const result = await coreApi.createAnonymousProfile(dataPayload);
|
|
86
|
-
return result.data
|
|
95
|
+
return result.data;
|
|
87
96
|
} catch (error) {
|
|
88
97
|
console.error('Could not create anonymous profile:', error);
|
|
89
|
-
return undefined
|
|
98
|
+
return undefined;
|
|
90
99
|
}
|
|
91
100
|
}
|
|
92
101
|
|
package/src/track.ts
CHANGED
|
@@ -12,6 +12,13 @@ import validator from '@/services/validator';
|
|
|
12
12
|
*/
|
|
13
13
|
const FP_TRIGGER_EVENTS = ['Search', 'View Results'];
|
|
14
14
|
|
|
15
|
+
const EXTRA_UTM_FIELDS = {
|
|
16
|
+
utm_brand: 'UTM Brand',
|
|
17
|
+
utm_kxconfig: 'UTM KXConfig',
|
|
18
|
+
gad_source: 'GAD Source',
|
|
19
|
+
gclid: 'GCLID',
|
|
20
|
+
};
|
|
21
|
+
|
|
15
22
|
/**
|
|
16
23
|
* Simplifies the structure of event data by flattening nested objects and arrays.
|
|
17
24
|
* @param {object} data - The event data to simplify.
|
|
@@ -81,11 +88,13 @@ export async function trackEvent(
|
|
|
81
88
|
const defaultProperties = {
|
|
82
89
|
'User Fingerprint': fingerprint,
|
|
83
90
|
};
|
|
91
|
+
const trackingParams = extractTrackingParams();
|
|
84
92
|
|
|
85
93
|
const simplifiedData = flattenEventData(eventProperties);
|
|
86
94
|
const properties = {
|
|
87
95
|
...defaultProperties,
|
|
88
96
|
...simplifiedData,
|
|
97
|
+
...trackingParams,
|
|
89
98
|
...meta,
|
|
90
99
|
};
|
|
91
100
|
|
|
@@ -94,3 +103,26 @@ export async function trackEvent(
|
|
|
94
103
|
console.error(`Error tracking event '${eventName}':`, error);
|
|
95
104
|
}
|
|
96
105
|
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Extracts UTM and other tracking parameters from the current URL that match predefined fields.
|
|
109
|
+
* @returns {Record<string, string>} An object containing matched tracking parameters.
|
|
110
|
+
*/
|
|
111
|
+
function extractTrackingParams(): Record<string, string> {
|
|
112
|
+
try {
|
|
113
|
+
const urlParams = new URLSearchParams(window.location.search);
|
|
114
|
+
const trackingData: Record<string, string> = {};
|
|
115
|
+
|
|
116
|
+
Object.entries(EXTRA_UTM_FIELDS).forEach(([queryParam, displayName]) => {
|
|
117
|
+
const value = urlParams.get(queryParam);
|
|
118
|
+
if (value) {
|
|
119
|
+
trackingData[displayName] = value;
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
return trackingData;
|
|
124
|
+
} catch (error) {
|
|
125
|
+
console.error('Error extracting tracking parameters:', error);
|
|
126
|
+
return {};
|
|
127
|
+
}
|
|
128
|
+
}
|