@telefonica/google-analytics
Advanced tools
Comparing version 1.0.3 to 2.0.0
@@ -1,2 +0,2 @@ | ||
import { sanitizeAnalyticsParam, sanitizeAnalyticsParams } from './sanitize.js'; | ||
import { sanitizeAnalyticsString, sanitizeAnalyticsParams, sanitizeAnalyticsMoneyAmount } from './sanitize.js'; | ||
import { consoleApi } from './api/console.js'; | ||
@@ -6,3 +6,3 @@ import { gtagApi } from './api/gtag.js'; | ||
export type { AnalyticsApi }; | ||
export { gtagApi, consoleApi, sanitizeAnalyticsParam, sanitizeAnalyticsParams }; | ||
export { gtagApi, consoleApi, sanitizeAnalyticsString, sanitizeAnalyticsParams, sanitizeAnalyticsMoneyAmount }; | ||
export type TrackingEvent = { | ||
@@ -9,0 +9,0 @@ name: string; |
@@ -1,5 +0,5 @@ | ||
import { sanitizeAnalyticsParam, sanitizeAnalyticsParams } from './sanitize.js'; | ||
import { sanitizeAnalyticsString, sanitizeAnalyticsParams, getSanitizationStrategy, sanitizeAnalyticsMoneyAmount, } from './sanitize.js'; | ||
import { consoleApi } from './api/console.js'; | ||
import { gtagApi } from './api/gtag.js'; | ||
export { gtagApi, consoleApi, sanitizeAnalyticsParam, sanitizeAnalyticsParams }; | ||
export { gtagApi, consoleApi, sanitizeAnalyticsString, sanitizeAnalyticsParams, sanitizeAnalyticsMoneyAmount }; | ||
let api = gtagApi; | ||
@@ -22,3 +22,3 @@ let handleInitFinished; | ||
return waitInitialization.then(() => { | ||
const sanitizedScreenName = sanitizeAnalyticsParam(screenName); | ||
const sanitizedScreenName = sanitizeAnalyticsString(screenName); | ||
if (sanitizedScreenName === currentScreenName) { | ||
@@ -33,3 +33,3 @@ return; | ||
return waitInitialization.then(() => { | ||
const { name, ...rest } = sanitizeAnalyticsParams(event); | ||
const { name, ...rest } = sanitizeAnalyticsParams(event, getSanitizationStrategy(event.name)); | ||
return api.logEvent(name, { | ||
@@ -42,3 +42,3 @@ ...rest, | ||
export const setUserProperty = (name, value) => { | ||
return waitInitialization.then(() => api.setUserProperty(name, sanitizeAnalyticsParam(value))); | ||
return waitInitialization.then(() => api.setUserProperty(name, sanitizeAnalyticsString(value))); | ||
}; |
@@ -1,2 +0,5 @@ | ||
export declare const sanitizeAnalyticsParam: (str: string) => string; | ||
export declare const sanitizeAnalyticsParams: (params: Record<string, unknown>) => Record<string, unknown>; | ||
export type SanitizationStrategy = 'sanitize_numbers' | 'ignore_numbers'; | ||
export declare const sanitizeAnalyticsMoneyAmount: (amount: number) => string; | ||
export declare const sanitizeAnalyticsString: (str: string) => string; | ||
export declare const sanitizeAnalyticsParams: (params: Record<string, unknown>, strategy?: SanitizationStrategy) => Record<string, unknown>; | ||
export declare const getSanitizationStrategy: (eventName: string) => SanitizationStrategy; |
@@ -7,5 +7,7 @@ const removeAccents = (str) => | ||
const EVENT_PARAM_VALUE_CHARS_LIMIT = 100; | ||
export const sanitizeAnalyticsParam = (str) => removeAccents(str) | ||
const isObject = (value) => typeof value === 'object' && !Array.isArray(value) && value !== null; | ||
export const sanitizeAnalyticsMoneyAmount = (amount) => Math.floor(amount * 100).toString(); | ||
export const sanitizeAnalyticsString = (str) => removeAccents(str) | ||
.toLocaleLowerCase() | ||
.replace(/[^a-z0-9\s\-_/|:]/g, '') // Remove all non allowed characters | ||
.replace(/[^a-z0-9\s\-_/|:]/g, '') // Remove all non-allowed characters | ||
.replace(/\s+/g, ' ') // Replace repeated whitespaces with a single space | ||
@@ -15,10 +17,43 @@ .trim() | ||
.slice(0, EVENT_PARAM_VALUE_CHARS_LIMIT); | ||
export const sanitizeAnalyticsParams = (params) => { | ||
const sanitizedParams = {}; | ||
Object.entries(params).forEach(([key, value]) => { | ||
const sanitizedKey = key.slice(0, EVENT_PARAM_NAME_CHARS_LIMIT); | ||
const sanitizedValue = typeof value === 'string' ? sanitizeAnalyticsParam(value) : value; | ||
sanitizedParams[sanitizedKey] = sanitizedValue; | ||
}); | ||
return sanitizedParams; | ||
const sanitizeAnalyticsParam = (param, strategy) => { | ||
if (typeof param === 'number' && strategy === 'sanitize_numbers') { | ||
return sanitizeAnalyticsString(param.toString()); | ||
} | ||
if (typeof param === 'string') { | ||
return sanitizeAnalyticsString(param); | ||
} | ||
if (isObject(param)) { | ||
return sanitizeAnalyticsParams(param, strategy); | ||
} | ||
if (Array.isArray(param)) { | ||
return param.map((item) => sanitizeAnalyticsParam(item, strategy)); | ||
} | ||
return param; | ||
}; | ||
export const sanitizeAnalyticsParams = (params, strategy = 'sanitize_numbers') => Object.fromEntries(Object.entries(params).map(([key, value]) => [ | ||
key.slice(0, EVENT_PARAM_NAME_CHARS_LIMIT), | ||
sanitizeAnalyticsParam(value, strategy), | ||
])); | ||
// Online sales events from: https://support.google.com/analytics/answer/9267735?hl=en | ||
const ecommerceEventNames = new Set([ | ||
'add_payment_info', | ||
'add_shipping_info', | ||
'add_to_cart', | ||
'add_to_wishlist', | ||
'begin_checkout', | ||
'purchase', | ||
'refund', | ||
'remove_from_cart', | ||
'select_item', | ||
'select_promotion', | ||
'view_cart', | ||
'view_item', | ||
'view_item_list', | ||
'view_promotion', | ||
]); | ||
export const getSanitizationStrategy = (eventName) => { | ||
if (ecommerceEventNames.has(eventName)) { | ||
return 'ignore_numbers'; | ||
} | ||
return 'sanitize_numbers'; | ||
}; |
{ | ||
"name": "@telefonica/google-analytics", | ||
"type": "module", | ||
"version": "1.0.3", | ||
"version": "2.0.0", | ||
"description": "", | ||
@@ -6,0 +6,0 @@ "exports": { |
@@ -27,2 +27,8 @@ # `@telefonica/google-analytics` | ||
- Money amounts must be normalized with `sanitizeAnalyticsMoneyAmount` function. This function will remove | ||
decimals exceeding two decimal places, multiply by 100 and convert the value to a string. If you don't do so | ||
and send a number, unexpected results may happen because the normalization described below will be executed | ||
before sending the event. This restriction does not apply to ecommerce events | ||
(https://support.google.com/analytics/answer/9267735?hl=en). | ||
- Events are normalized before being sent to Google Analytics: | ||
@@ -35,2 +41,4 @@ | ||
- The same normalization is applied to screen names. | ||
- For non ecommerce events, numbers are converted to strings and then string normalization is applied. That | ||
means if there are dots (as in decimal numbers) they will be removed. | ||
@@ -37,0 +45,0 @@ - If `setScreenName` is called multiple times with the same screen name, only the first call will be sent to |
13546
225
76