Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 42 additions & 4 deletions kits/rokt/src/Rokt-Kit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
import { isLocalStorageAvailable } from './storage';

import { isObject, isString, isEmpty, isFunction, sanitizeUrl } from './utils';
import { buildSetterDiagnosticLogEntry, buildSelectPlacementsDiagnosticLogEntry } from './diagnosticTiming';
import {
createLauncherAttachState,
markLauncherAttached,
Expand Down Expand Up @@ -689,9 +690,18 @@ class ErrorReportingService {
}

class LoggingService {
private _transport: ReportingTransport;
private _loggingUrl: string;
private _errorReportingService: { report: (e: ErrorReport) => void };
private readonly _transport: ReportingTransport;
// Own ReportingTransport (and thus own RateLimiter) so a burst of
// diagnostic timing entries can't starve the operational INFO budget
// that _transport shares with page-view/quota logging via log().
private readonly _diagnosticTransport: ReportingTransport;
// Separate again from _diagnosticTransport: a burst of setter/identity
// calls (e.g. setUserAttributes looping per key) must not exhaust the
// budget a selectPlacements dispatch needs, or placement diagnostics go
// silent for the rest of the session.
private readonly _placementDiagnosticTransport: ReportingTransport;
private readonly _loggingUrl: string;
private readonly _errorReportingService: { report: (e: ErrorReport) => void };

constructor(
config: ReportingConfig,
Expand All @@ -702,13 +712,34 @@ class LoggingService {
rateLimiter?: RateLimiter,
) {
this._transport = new ReportingTransport(config, integrationName, launcherInstanceGuid, accountId, rateLimiter);
this._diagnosticTransport = new ReportingTransport(config, integrationName, launcherInstanceGuid, accountId);
this._placementDiagnosticTransport = new ReportingTransport(
config,
integrationName,
launcherInstanceGuid,
accountId,
);
this._loggingUrl = generateReportingUrl(config?.loggingUrl, config?.integrationDomain, LOGGING_ENDPOINT);
this._errorReportingService = errorReportingService;
}

log(entry: LogEntry | null | undefined): void {
if (!entry) return;
this._transport.send(
this._send(this._transport, entry);
}

logDiagnostic(entry: LogEntry | null | undefined): void {
if (!entry) return;
this._send(this._diagnosticTransport, entry);
Comment thread
cursor[bot] marked this conversation as resolved.
}

logPlacementDiagnostic(entry: LogEntry | null | undefined): void {
if (!entry) return;
this._send(this._placementDiagnosticTransport, entry);
}

private _send(transport: ReportingTransport, entry: LogEntry): void {
transport.send(
this._loggingUrl,
WSDKErrorSeverity.INFO,
entry.message,
Expand Down Expand Up @@ -1335,19 +1366,22 @@ class RoktKit implements KitInterface {
}

public setUserAttribute(key: string, value: unknown): string {
this.loggingService?.logDiagnostic(buildSetterDiagnosticLogEntry('setUserAttribute', [key]));
if (!isSelectPlacementsAttributePersistenceDenied(key)) {
this.userAttributes[key] = value;
}
return 'Successfully set user attribute for forwarder: ' + name;
}

public removeUserAttribute(key: string): string {
this.loggingService?.logDiagnostic(buildSetterDiagnosticLogEntry('removeUserAttribute', [key]));
delete this.userAttributes[key];
return 'Successfully removed user attribute for forwarder: ' + name;
}

private handleIdentityComplete(user: IMParticleUser, callbackName: string): string {
this.userAttributes = removeSelectPlacementsAttributePersistenceDeniedAttributes(user.getAllUserAttributes());
this.loggingService?.logDiagnostic(buildSetterDiagnosticLogEntry(callbackName, Object.keys(this.userAttributes)));
return 'Successfully called ' + callbackName + ' for forwarder: ' + name;
}

Expand Down Expand Up @@ -1552,6 +1586,10 @@ class RoktKit implements KitInterface {

const selectPlacementsOptions: Record<string, unknown> = { ...options, attributes: selectPlacementsAttributes };

this.loggingService?.logPlacementDiagnostic(
buildSelectPlacementsDiagnosticLogEntry(Object.keys(selectPlacementsAttributes)),
);

const selection = this.launcher!.selectPlacements(selectPlacementsOptions);

// After selection resolves, sync the Rokt session ID back to mParticle, then log
Expand Down
26 changes: 26 additions & 0 deletions kits/rokt/src/diagnosticTiming.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Builds diagnostic log entries for attribute/identity setter calls and
// selectPlacements dispatches. Each fires independently at the moment it
// happens; correlating the two (timing delta, which setters preceded a given
// placement call) is done downstream from the logged timestamps and page URL
// that ReportingTransport already attaches to every log request. Attribute
// names only — never values — since this ships over the network logging
// pipeline and setter payloads can carry customer PII.

export interface DiagnosticLogEntry {
message: string;
code: string;
}

export function buildSetterDiagnosticLogEntry(source: string, attributeKeys: string[]): DiagnosticLogEntry {
return {
message: `Rokt Kit: ${source} called [attributeKeys=${attributeKeys.join(',')}]`,
code: 'ATTRIBUTE_SETTER_CALLED',
};
}

export function buildSelectPlacementsDiagnosticLogEntry(placementAttributeKeys: string[]): DiagnosticLogEntry {
return {
message: `Rokt Kit: selectPlacements dispatched [placementAttributeKeys=${placementAttributeKeys.join(',')}]`,
code: 'SELECT_PLACEMENTS_DISPATCHED',
};
}
34 changes: 34 additions & 0 deletions kits/rokt/test/src/diagnosticTiming.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { describe, it, expect } from 'vitest';
import { buildSetterDiagnosticLogEntry, buildSelectPlacementsDiagnosticLogEntry } from '../../src/diagnosticTiming';

describe('diagnosticTiming', () => {
describe('buildSetterDiagnosticLogEntry', () => {
it('reports the source and attribute keys', () => {
const entry = buildSetterDiagnosticLogEntry('setUserAttribute', ['favoriteColor']);

expect(entry.code).toBe('ATTRIBUTE_SETTER_CALLED');
expect(entry.message).toBe('Rokt Kit: setUserAttribute called [attributeKeys=favoriteColor]');
});

it('joins multiple attribute keys', () => {
const entry = buildSetterDiagnosticLogEntry('onUserIdentified', ['email', 'firstName']);

expect(entry.message).toContain('[attributeKeys=email,firstName]');
});

it('never includes attribute values, only keys', () => {
const entry = buildSetterDiagnosticLogEntry('setUserAttribute', ['email']);

expect(entry.message).not.toContain('test@example.com');
});
});

describe('buildSelectPlacementsDiagnosticLogEntry', () => {
it('reports the full set of placement attribute keys', () => {
const entry = buildSelectPlacementsDiagnosticLogEntry(['favoriteColor', 'mpid']);

expect(entry.code).toBe('SELECT_PLACEMENTS_DISPATCHED');
expect(entry.message).toBe('Rokt Kit: selectPlacements dispatched [placementAttributeKeys=favoriteColor,mpid]');
});
});
});
Loading
Loading