You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/@ama-mfe/ng-utils/README.md
+154Lines changed: 154 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -295,3 +295,157 @@ The host information is stored in session storage so it won't be lost when navig
295
295
When using iframes to embed applications, the browser history might be shared by the main page and the embedded iframe. For example `<iframe src="https://example.com" sandbox="allow-same-origin">` will share the same history as the main page. This can lead to unexpected behavior when using browser 'back' and 'forward' buttons.
296
296
297
297
To avoid this, the `@ama-mfe/ng-utils` will forbid the application running in the iframe to alter the browser history. It will happen when connection is configured using `provideConection()` function. This will prevent the iframe to be able to use the `history.pushState` and `history.replaceState` methods.
298
+
299
+
### User Activity Tracking
300
+
301
+
The User Activity Tracking feature allows a host application (shell) to monitor user interactions across embedded micro-frontends. This is useful for implementing session timeout functionality, analytics, or any feature that needs to know when users are actively interacting with the application.
302
+
303
+
#### How it works
304
+
305
+
-**Producer**: The `ActivityProducerService` listens for DOM events (click, keydown, scroll, touchstart, focus) and:
306
+
- Exposes a `localActivity` signal for consumers within the same application (not throttled for local detection).
307
+
- Sends throttled activity messages via the communication protocol to connected peers.
308
+
-**Consumer**: The `ActivityConsumerService` receives activity messages from connected peers via the communication protocol and exposes them via the `latestReceivedActivity` signal.
309
+
310
+
Both services can be used in either the shell (host) or embedded applications depending on your use case. For example:
311
+
- A shell can produce activity signals to notify embedded modules of user interactions in the host.
312
+
- An embedded module can consume activity signals from the shell.
313
+
- An application can use the producer's `localActivity` signal to detect activity locally.
314
+
315
+
The service automatically throttles messages sent to peers to prevent flooding the communication channel. High-frequency events like `scroll` have additional throttling.
316
+
317
+
#### Producer Configuration
318
+
319
+
Start the `ActivityProducerService` to send activity signals to connected peers:
throttleMs: 5000// Send at most one message every 5 seconds
334
+
});
335
+
}
336
+
});
337
+
});
338
+
```
339
+
340
+
##### Configuration Options
341
+
342
+
| Option | Type | Default | Description |
343
+
|--------|------|---------|-------------|
344
+
|`throttleMs`|`number`|`1000`| Minimum interval between activity messages sent to the host |
345
+
|`trackNestedIframes`|`boolean`|`false`| Enable tracking of nested iframes within the application |
346
+
|`nestedIframePollIntervalMs`|`number`|`1000`| Polling interval for detecting iframe focus changes |
347
+
|`nestedIframeActivityEmitIntervalMs`|`number`|`30000`| Interval for sending activity signals while an iframe has focus |
348
+
|`highFrequencyThrottleMs`|`number`|`300`| Throttle time for high-frequency events (scroll) |
349
+
|`shouldBroadcast`|`(event: Event) => boolean`| - | Optional filter function to control which events are broadcast |
350
+
351
+
##### Filtering Events with shouldBroadcast
352
+
353
+
The `shouldBroadcast` option allows you to filter which events trigger activity messages. This is useful in the shell application to exclude events that occur on iframes, since user activity inside embedded modules is already tracked via the communication protocol.
354
+
355
+
```typescript
356
+
// In the shell application
357
+
inject(ActivityProducerService).start({
358
+
throttleMs: 1000,
359
+
shouldBroadcast: (event:Event) => {
360
+
// Exclude events on iframes - activity from embedded modules comes via the communication protocol
361
+
return!(event.targetinstanceofHTMLIFrameElement);
362
+
}
363
+
});
364
+
```
365
+
366
+
##### Tracking Nested Iframes
367
+
368
+
When a user interacts with content inside an iframe (e.g., a third-party widget, payment form, or embedded content), the parent application cannot detect those interactions directly. This is because:
369
+
370
+
1.**Cross-origin restrictions**: DOM events inside cross-origin iframes do not bubble up to the parent document.
371
+
2.**Focus isolation**: When an iframe has focus, the parent document stops receiving keyboard and mouse events.
372
+
373
+
This creates a problem for detection of user interactions: a user could be actively filling out a form inside an iframe, but the host application would see no activity and might incorrectly trigger a session timeout.
374
+
375
+
**Solution**: When `trackNestedIframes` is enabled, the `ActivityProducerService` polls `document.activeElement` to detect when an iframe gains focus. While an iframe has focus, the service simulates activity by periodically emitting `iframeinteraction` events. This ensures the host application knows the user is still active, even though it cannot see the actual interactions inside the iframe.
376
+
377
+
Enable nested iframe tracking in embedded applications that contain other iframes:
378
+
379
+
```typescript
380
+
inject(ActivityProducerService).start({
381
+
throttleMs: 5000,
382
+
trackNestedIframes: true,
383
+
nestedIframePollIntervalMs: 1000, // Check for iframe focus every second
384
+
nestedIframeActivityEmitIntervalMs: 30000// Send activity every 30s while iframe has focus
385
+
});
386
+
```
387
+
388
+
**When to use**: Enable this option in embedded modules that contain iframes whose content you cannot modify to include activity tracking (e.g., third-party widgets, payment providers, or external content).
389
+
390
+
#### Consumer Configuration
391
+
392
+
Start the `ActivityConsumerService` to receive activity signals from connected peers:
0 commit comments