From 40e15871da7f84b2388f14fd441124418fab2b5c Mon Sep 17 00:00:00 2001 From: Roger Albino Date: Mon, 8 Dec 2025 19:33:48 -0300 Subject: [PATCH 1/5] feat: add logging when publisherId is missing in VTEXAdsProvider --- react/components/VTEXAdsProvider.tsx | 5 + react/utils/sendLog.ts | 140 +++++++++++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 react/utils/sendLog.ts diff --git a/react/components/VTEXAdsProvider.tsx b/react/components/VTEXAdsProvider.tsx index 5ce8b69e..a3676da8 100644 --- a/react/components/VTEXAdsProvider.tsx +++ b/react/components/VTEXAdsProvider.tsx @@ -10,6 +10,7 @@ import { getUserData, SessionResponse, } from '../hooks/getUserData' +import { sendLog } from '../utils/sendLog' interface VTEXAdsProviderProps { children?: React.ReactNode @@ -93,6 +94,10 @@ export const VTEXAdsProvider = ({ children }: VTEXAdsProviderProps) => { } if (!publisherId) { + sendLog({ + message: 'PublisherId não encontrado no VTEXAdsProvider', + body: `PublisherId ausente para a conta: ${account}` + }).catch(() => {}) return children } diff --git a/react/utils/sendLog.ts b/react/utils/sendLog.ts new file mode 100644 index 00000000..cab0f074 --- /dev/null +++ b/react/utils/sendLog.ts @@ -0,0 +1,140 @@ +interface SendLogOptions { + message: string + searchIndex?: string + environment?: string + body?: string +} + +export async function sendLog( + options: SendLogOptions +): Promise { + const { + message, + searchIndex = 'ads_pai', + environment = 'production', + body + } = options + + const severityText = 'ERROR' + const severityNumber = 17 + const timeUnixNano = (Date.now() * 1000000).toString() + const observedTimeUnixNano = timeUnixNano + + const traceId = [...crypto.getRandomValues(new Uint8Array(16))] + .map(b => b.toString(16).padStart(2, '0')) + .join('') + + const spanId = [...crypto.getRandomValues(new Uint8Array(8))] + .map(b => b.toString(16).padStart(2, '0')) + .join('') + + let hostname = '' + let path = '' + let url = '' + + if (typeof window !== 'undefined' && window.location) { + hostname = window.location.hostname + path = window.location.pathname + url = window.location.href + } + + const payload = { + resourceLogs: [ + { + resource: { + attributes: [ + { + key: '_msg', + value: { + stringValue: message || 'Log message' + } + }, + { + key: 'hostname', + value: { + stringValue: hostname + } + }, + { + key: 'path', + value: { + stringValue: path + } + }, + { + key: 'environment', + value: { + stringValue: environment + } + }, + { + key: 'browserInfos', + value: { + stringValue: JSON.stringify({ + url: url, + hostname: hostname, + path: path + }) + } + }, + { + key: 'searchParams', + value: { + stringValue: + typeof window !== 'undefined' && window.location + ? window.location.search + : '' + } + } + ] + }, + scopeLogs: [ + { + scope: {}, + logRecords: [ + { + timeUnixNano: timeUnixNano, + observedTimeUnixNano: observedTimeUnixNano, + severityText: severityText, + severityNumber: severityNumber, + attributes: [ + { + key: 'vtex.search_index', + value: { + stringValue: searchIndex + } + }, + { + key: 'body', + value: { + stringValue: body || message || 'Log message' + } + } + ], + traceId: traceId, + spanId: spanId + } + ] + } + ] + } + ] + } + + try { + const response = await fetch( + 'https://stable.vtexobservability.com/v1/logs', + { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify(payload) + } + ) + return response + } catch (error) { + console.error('Erro ao enviar log:', error) + } +} + From ad3a77da77a636adfe4e90738262e5982b69cacb Mon Sep 17 00:00:00 2001 From: Roger Albino Date: Mon, 8 Dec 2025 19:38:20 -0300 Subject: [PATCH 2/5] docs: add changelog entry for publisherId logging feature --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7be466da..8f00ad98 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Added + +- Logging functionality to track domains missing `publisherId` configuration in VTEXAdsProvider + ## [2.145.0] - 2025-12-04 ### Added From be34d586e581c1655ab688a7aacc625b4100e5ca Mon Sep 17 00:00:00 2001 From: Roger Albino Date: Mon, 8 Dec 2025 20:17:18 -0300 Subject: [PATCH 3/5] refactor: fix linting issues in sendLog.ts (trailing commas and property shorthand) --- react/utils/sendLog.ts | 75 +++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 38 deletions(-) diff --git a/react/utils/sendLog.ts b/react/utils/sendLog.ts index cab0f074..65a72d31 100644 --- a/react/utils/sendLog.ts +++ b/react/utils/sendLog.ts @@ -12,7 +12,7 @@ export async function sendLog( message, searchIndex = 'ads_pai', environment = 'production', - body + body, } = options const severityText = 'ERROR' @@ -46,36 +46,36 @@ export async function sendLog( { key: '_msg', value: { - stringValue: message || 'Log message' - } + stringValue: message ?? 'Log message', + }, }, { key: 'hostname', value: { - stringValue: hostname - } + stringValue: hostname, + }, }, { key: 'path', value: { - stringValue: path - } + stringValue: path, + }, }, { key: 'environment', value: { - stringValue: environment - } + stringValue: environment, + }, }, { key: 'browserInfos', value: { stringValue: JSON.stringify({ - url: url, - hostname: hostname, - path: path - }) - } + url, + hostname, + path, + }), + }, }, { key: 'searchParams', @@ -83,42 +83,42 @@ export async function sendLog( stringValue: typeof window !== 'undefined' && window.location ? window.location.search - : '' - } - } - ] + : '', + }, + }, + ], }, scopeLogs: [ { scope: {}, logRecords: [ { - timeUnixNano: timeUnixNano, - observedTimeUnixNano: observedTimeUnixNano, - severityText: severityText, - severityNumber: severityNumber, + timeUnixNano, + observedTimeUnixNano, + severityText, + severityNumber, attributes: [ { key: 'vtex.search_index', value: { - stringValue: searchIndex - } + stringValue: searchIndex, + }, }, { key: 'body', value: { - stringValue: body || message || 'Log message' - } - } + stringValue: body ?? message ?? 'Log message', + }, + }, ], - traceId: traceId, - spanId: spanId - } - ] - } - ] - } - ] + traceId, + spanId, + }, + ], + }, + ], + }, + ], } try { @@ -127,9 +127,9 @@ export async function sendLog( { method: 'POST', headers: { - 'Content-Type': 'application/json' + 'Content-Type': 'application/json', }, - body: JSON.stringify(payload) + body: JSON.stringify(payload), } ) return response @@ -137,4 +137,3 @@ export async function sendLog( console.error('Erro ao enviar log:', error) } } - From a45ad4a066094b18d596969e6627a65989d7e138 Mon Sep 17 00:00:00 2001 From: Roger Albino Date: Mon, 8 Dec 2025 20:22:20 -0300 Subject: [PATCH 4/5] fix: add trailing comma in sendLog call and fix condition logic --- react/components/VTEXAdsProvider.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/react/components/VTEXAdsProvider.tsx b/react/components/VTEXAdsProvider.tsx index a3676da8..c5cbf95f 100644 --- a/react/components/VTEXAdsProvider.tsx +++ b/react/components/VTEXAdsProvider.tsx @@ -96,7 +96,7 @@ export const VTEXAdsProvider = ({ children }: VTEXAdsProviderProps) => { if (!publisherId) { sendLog({ message: 'PublisherId não encontrado no VTEXAdsProvider', - body: `PublisherId ausente para a conta: ${account}` + body: `PublisherId ausente para a conta: ${account}`, }).catch(() => {}) return children } From 839586de1d5859746c8b837dbee133d559ce7e70 Mon Sep 17 00:00:00 2001 From: Roger Albino Date: Tue, 9 Dec 2025 11:21:30 -0300 Subject: [PATCH 5/5] refactor: send error log --- react/components/VTEXAdsProvider.tsx | 5 +++-- react/utils/{sendLog.ts => sendErrorLog.ts} | 13 ++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) rename react/utils/{sendLog.ts => sendErrorLog.ts} (95%) diff --git a/react/components/VTEXAdsProvider.tsx b/react/components/VTEXAdsProvider.tsx index c5cbf95f..ef2d843a 100644 --- a/react/components/VTEXAdsProvider.tsx +++ b/react/components/VTEXAdsProvider.tsx @@ -10,7 +10,7 @@ import { getUserData, SessionResponse, } from '../hooks/getUserData' -import { sendLog } from '../utils/sendLog' +import { sendErrorLog } from '../utils/sendErrorLog' interface VTEXAdsProviderProps { children?: React.ReactNode @@ -94,9 +94,10 @@ export const VTEXAdsProvider = ({ children }: VTEXAdsProviderProps) => { } if (!publisherId) { - sendLog({ + sendErrorLog({ message: 'PublisherId não encontrado no VTEXAdsProvider', body: `PublisherId ausente para a conta: ${account}`, + searchIndex: 'ads_pai', }).catch(() => {}) return children } diff --git a/react/utils/sendLog.ts b/react/utils/sendErrorLog.ts similarity index 95% rename from react/utils/sendLog.ts rename to react/utils/sendErrorLog.ts index 65a72d31..daa2a7a0 100644 --- a/react/utils/sendLog.ts +++ b/react/utils/sendErrorLog.ts @@ -5,15 +5,14 @@ interface SendLogOptions { body?: string } -export async function sendLog( +export async function sendErrorLog( options: SendLogOptions ): Promise { - const { - message, - searchIndex = 'ads_pai', - environment = 'production', - body, - } = options + if (!options?.searchIndex) { + return null + } + + const { message, searchIndex, environment = 'production', body } = options const severityText = 'ERROR' const severityNumber = 17