Analytics
Analytics scripting object that captures business analytics events and performance timing measurements, forwarding them to multiple destinations.
Destinations
| Destination | Transport | What it receives |
|---|---|---|
| Splunk | logger.info() | Timing events only — message, event, duration, startTime, detail |
| GTM | window.gtmDataLayer.push() | BA events and timing events — full enriched payload (BA params + product details + caller data) |
| LogRocket | logRocket.track() via lrTrackGuarded | BA events only: caller-supplied fields |
Use cases
1. Business analytics events (sendBAEvent)
Fire-and-forget event tracking for user actions, page views, feature usage, errors, etc. No sampling — every call reaches GTM and LogRocket (subject to LR throttle). GTM receives the full enriched context; LogRocket receives only the caller-supplied fields to conserve its 2,000-property session budget.
2. Manual performance timing (startTiming / endTiming)
Bracket a unit of work to measure its wall-clock duration. startTiming
creates a performance.mark(); endTiming calls performance.measure()
which is then picked up by the automatic pipeline (use case 3).
3. Automatic performance observation (#monitorPerformance)
A page-wide PerformanceObserver captures every performance.measure()
entry — including those from endTiming and any other code on the page.
Each entry passes through:
- Third-party filter — GTM/GA4 internal measures (names matching
GTM-*orG-*) are silently dropped. - Head sampling — per-event-name ratio (default 25%) decides whether
to record. Configurable via
setTimingEventSamplingRatio. - Fan-out — sampled entries are sent to Splunk and GTM.
Processing is deferred via
queueMicrotaskso the caller'sperformance.measure()returns immediately.
4. Sampling ratio management
Runtime CRUD for per-event sampling ratios via setTimingEventSamplingRatio,
getTimingEventSamplingRatio, deleteTimingEventSamplingRatio, and
getAllTimingEventSamplingRatios. Sampling applies uniformly to both
timing destinations (Splunk and GTM) — there is no per-destination sampling.
Example
const analytics = new Analytics({
logger,
timingEventSamplingRatios: { 'api-call': 0.1, 'page-load': 1.0 },
});
// Business event
await analytics.sendBAEvent({ event: 'user-login', method: 'oauth' });
// Timing measurement
const mark = await analytics.startTiming('api-call', { appId: 'svc', appUrl: location.href });
await fetchData();
await analytics.endTiming(mark, { appId: 'svc', appUrl: location.href });
Extends
ScriptingObject
Implements
IAnalytics
Accessors
id
Get Signature
get id(): string;
get unique id of the scripting object
Returns
string
Implementation of
IAnalytics.id
Inherited from
ScriptingObject.id
objectType
Get Signature
get objectType(): string;
get type of the scripting object
Returns
string
Implementation of
IAnalytics.objectType
Inherited from
ScriptingObject.objectType
Constructors
Constructor
new Analytics(params: AnalyticsParams): Analytics;
Creates a new Analytics scripting object and immediately starts the page-wide PerformanceObserver for timing events.
Parameters
params
AnalyticsParams
Analytics configuration
Returns
Analytics
Overrides
ScriptingObject.constructor
Methods
deleteTimingEventSamplingRatio()
deleteTimingEventSamplingRatio(names: string[]): void;
Remove explicit sampling configuration for the given timing event names.
After deletion the events revert to the default ratio of 0.25 (25%). Deleting a name that was never configured is a no-op.
Parameters
names
string[]
Array of event names whose ratios should be removed
Returns
void
Example
analytics.deleteTimingEventSamplingRatio(['api-call', 'user-interaction']);
// Both now sample at the default 25%
Implementation of
IAnalytics.deleteTimingEventSamplingRatio
endTiming()
endTiming(start: string | PerfMark, options: TimingOptions): Promise<void>;
End a performance timing measurement by calling performance.measure().
The resulting measure entry is automatically captured by the PerformanceObserver pipeline, which filters, samples, and fans out to Splunk and GTM. The mark is cleared after measurement.
What each destination receives
| Destination | Event name | Payload |
|---|---|---|
| Splunk | entry.name (the measurement name) | { message: 'timing', event, duration, startTime, detail } |
| GTM | timing:<name> | BA params + product details + { duration, startTime, startAppId, startAppUrl, endAppId, endAppUrl } |
Accepted start formats
PerfMark(return value of startTiming) — usesstartTimefor sub-millisecond accuracy even if the mark was cleared externally.string(the name passed to startTiming) — looks up the mark by name; falls back toperformance.now()if the mark is missing.
Parameters
start
The PerfMark returned by startTiming, or the name string
string | PerfMark
options
TimingOptions
Must include appId and appUrl for the end context
Returns
Promise<void>
A pre-resolved promise (for interface compatibility)
Throws
If start is falsy or options.appId / options.appUrl are missing
Examples
// Using the PerfMark object (recommended)
const mark = await analytics.startTiming('LongTask', { appId: 'myApp', appUrl: 'https://myapp.com' });
await doWork();
await analytics.endTiming(mark, { appId: 'myApp', appUrl: 'https://myapp.com' });
// Using the name string (cross-app timing — start and end may differ)
await analytics.startTiming('LongTask', { appId: 'appA', appUrl: 'https://appA.com' });
await doWork();
await analytics.endTiming('LongTask', { appId: 'appB', appUrl: 'https://appB.com' });
Implementation of
IAnalytics.endTiming
getAllTimingEventSamplingRatios()
getAllTimingEventSamplingRatios(): Record<string, number>;
Return a snapshot of all explicitly configured timing event sampling ratios.
Events using the default ratio (0.25) are not included. The returned object is a copy — mutating it does not affect the internal state.
Returns
Record<string, number>
Map of event name to ratio for all explicitly configured events
Example
const ratios = analytics.getAllTimingEventSamplingRatios();
// { 'api-call': 0.1, 'user-interaction': 0.5 }
Implementation of
IAnalytics.getAllTimingEventSamplingRatios
getTimingEventSamplingRatio()
getTimingEventSamplingRatio(names: string[]): Record<string, number | undefined>;
Retrieve the current sampling ratios for the specified timing event names.
Returns undefined for events that have no explicit ratio configured
(i.e. they use the default 0.25).
Parameters
names
string[]
Array of event names to query
Returns
Record<string, number | undefined>
Map of event name to ratio, or undefined if using default
Example
const ratios = analytics.getTimingEventSamplingRatio(['api-call', 'page-load']);
// { 'api-call': 0.1, 'page-load': undefined }
Implementation of
IAnalytics.getTimingEventSamplingRatio
sendBAEvent()
sendBAEvent(eve: BAEvent): Promise<void>;
Send a business analytics event to GTM and LogRocket.
No sampling or head-sampling is applied — every call reaches both destinations (subject to LogRocket's sliding-window throttle).
GTM properties (gtmDataLayer.push)
| Property | Source |
|---|---|
event | Caller-supplied event field |
envName | sessionStorage.getItem('envName') |
appId | window.emui.appId |
instanceId | sessionStorage.getItem('instanceId') |
userId | sessionStorage.getItem('userId') |
...appParameters | Fields set via updateBAEventParameters() |
productId | window.parent.emui.appId |
productPath | window.parent.location.pathname |
productUrl | window.parent.location.href |
productPageTitle | window.parent.document.title |
version | window.parent.emui.version |
appPath | window.location.pathname |
appUrl | window.location.href |
appPageTitle | document.title |
...rest | All other caller-supplied fields |
LogRocket properties (logRocket.track)
| Property | Source |
|---|---|
| Track event name | Caller-supplied event field |
...rest | All caller-supplied fields except event |
No ambient context (envName, appId, instanceId, userId, product
details, appParameters) is sent to LogRocket.
Parameters
eve
BAEvent
Business analytics event. Must contain an event string field; all other fields are forwarded as-is. Returns immediately if event is falsy.
Returns
Promise<void>
A pre-resolved promise (for interface compatibility)
Example
// User action tracking
await analytics.sendBAEvent({ event: 'button-click', buttonId: 'submit' });
// Page view tracking
await analytics.sendBAEvent({ event: 'page-view', page: '/dashboard' });
// Feature usage
await analytics.sendBAEvent({ event: 'feature-used', feature: 'export-pdf' });
// Error tracking
await analytics.sendBAEvent({ event: 'api-error', endpoint: '/users', status: 500 });
Implementation of
IAnalytics.sendBAEvent
setTimingEventSamplingRatio()
setTimingEventSamplingRatio(ratios: Record<string, number>): void;
Set or update sampling ratios for one or more timing event names.
Sampling applies uniformly to both timing destinations (Splunk and GTM) — there is no per-destination sampling. Events not configured here fall back to the default ratio of 0.25 (25%).
Parameters
ratios
Record<string, number>
Map of event name to ratio (0 = never, 1 = always)
Returns
void
Example
analytics.setTimingEventSamplingRatio({
'api-call': 0.1, // 10% — high-frequency, keep volume low
'page-load': 1.0, // 100% — critical, capture every instance
'user-interaction': 0.5 // 50%
});
Implementation of
IAnalytics.setTimingEventSamplingRatio
startTiming()
startTiming(name: string, options: TimingOptions): Promise<{
name: string;
startTime: number;
}>;
Start a performance timing measurement by creating a performance.mark().
The returned PerfMark (or the name string) is later passed to
endTiming which calls performance.measure(). That measure is
automatically picked up by the PerformanceObserver pipeline, which
samples and fans out to Splunk and GTM.
If a mark with the same name already exists it will be replaced by the
browser's Performance API.
Parameters
name
string
Unique name for the measurement (e.g. 'api-call', 'page-load')
options
TimingOptions
Must include appId and appUrl; stored as detail on the mark
Returns
Promise<{
name: string;
startTime: number;
}>
A promise resolving to { name, startTime } for passing to endTiming
Throws
If name is falsy or options.appId / options.appUrl are missing
Example
const mark = await analytics.startTiming('api-call', {
appId: 'loan-service',
appUrl: 'https://app.example.com/loans',
});
// ... perform work ...
await analytics.endTiming(mark, { appId: 'loan-service', appUrl: 'https://app.example.com/loans' });
Implementation of
IAnalytics.startTiming
Properties
_dispose()
_dispose: () => void;
dispose the scripting object
Returns
void
Implementation of
IAnalytics._dispose
Inherited from
ScriptingObject._dispose
_toJSON()
_toJSON: () => RemotingScriptingObject;
transform the scripting object to a format suitable for transmitting over window.postMessage
Returns
RemotingScriptingObject
marshalled scripting object
Implementation of
IAnalytics._toJSON
Inherited from
ScriptingObject._toJSON
dispose()
dispose: () => void;
dispose the scripting object
Returns
void
Inherited from
ScriptingObject.dispose