-
Notifications
You must be signed in to change notification settings - Fork 36
[Angular CSDK] Multisite, personalize and events #516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
art-alexeyenko
merged 14 commits into
release/beta-angular
from
feature/jss-8962-ang-multi-pers
Jun 18, 2026
Merged
Changes from 8 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b60240f
personalize+mulitiste middleware. next: wire middleware results to lo…
art-alexeyenko 3832d92
ensure passthrough flow for site, variantIds
art-alexeyenko 58a4a09
CLI config to use angualr config, wire personalize,multisite middlewa…
art-alexeyenko be5ef20
separate config submodule export in angular pkg
art-alexeyenko 94ca327
add event tracking support
art-alexeyenko 911c80c
Ensure multisite and personalize flow on browser nav
art-alexeyenko 9548858
Merge branch 'release/beta-angular' of https://github.com/Sitecore/co…
art-alexeyenko 198668b
api coverage and changeset
art-alexeyenko 55d85ec
stop using fetch API types for 'useCookieResolution`
art-alexeyenko 2e189c1
address other PR comments
art-alexeyenko e129cd4
PR comments part 2
art-alexeyenko 90459bb
lint
art-alexeyenko 66bd707
api surface
art-alexeyenko a6c626c
Pr comments 3
art-alexeyenko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@sitecore-content-sdk/angular': minor | ||
| --- | ||
|
|
||
| Personalize, multisite and analytics support |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "$schema": "../node_modules/ng-packagr/ng-entrypoint.schema.json", | ||
| "lib": { | ||
| "entryFile": "../src/config/index.ts" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
packages/angular/src/lib/analytics/sitecore-analytics.browser.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| import { Injectable, inject, isDevMode } from '@angular/core'; | ||
| import { initContentSdk } from '@sitecore-content-sdk/core'; | ||
| import { analyticsBrowserAdapter, analyticsPlugin } from '@sitecore-content-sdk/analytics-core'; | ||
| import { event, eventsPlugin, form, identity, pageView } from '@sitecore-content-sdk/events'; | ||
| import type { EventData, IdentityData, PageViewData } from '@sitecore-content-sdk/events'; | ||
| import { SITECORE_CONFIG_TOKEN } from '../tokens'; | ||
| import { normalizeCookieDomain, type SitecoreAnalyticsWrapper } from './sitecore-analytics'; | ||
| import debug from '../../debug'; | ||
|
|
||
| /** Dedup window for identical page-view fingerprints (hydration replay, transfer-state restore). */ | ||
| const DEDUP_WINDOW_MS = 1000; | ||
|
|
||
| /** | ||
| * Browser {@link SitecoreAnalyticsWrapper} implementation. Lazily initializes the Content SDK | ||
| * events runtime on first dispatch — memoized so concurrent first callers await the same promise | ||
| * — then forwards to the `@sitecore-content-sdk/events` functions. Failures are swallowed at debug | ||
| * level: analytics must never break the app. | ||
| * @public | ||
| */ | ||
| @Injectable() | ||
| export class SitecoreAnalyticsBrowser implements SitecoreAnalyticsWrapper { | ||
| private readonly config = inject(SITECORE_CONFIG_TOKEN, { optional: true }); | ||
| /** Memoized one-time init; resolves `false` when analytics is disabled (dev / missing Edge). */ | ||
| private initPromise?: Promise<boolean>; | ||
| /** Last page-view fingerprint + timestamp, for same-payload dedup. */ | ||
| private lastPageView?: { fingerprint: string; at: number }; | ||
|
|
||
| pageView(data: PageViewData): Promise<void> { | ||
| const fingerprint = `${data.page ?? ''}|${data.pageVariantId ?? ''}|${data.language ?? ''}`; | ||
| const now = Date.now(); | ||
| if ( | ||
| this.lastPageView?.fingerprint === fingerprint && | ||
| now - this.lastPageView.at < DEDUP_WINDOW_MS | ||
| ) { | ||
| debug.common('analytics pageView deduped: %s', fingerprint); | ||
| return Promise.resolve(); | ||
| } | ||
| this.lastPageView = { fingerprint, at: now }; | ||
| return this.dispatch(() => pageView(data)); | ||
| } | ||
|
|
||
| event(data: EventData): Promise<void> { | ||
| return this.dispatch(() => event(data)); | ||
| } | ||
|
|
||
| identity(data: IdentityData): Promise<void> { | ||
| return this.dispatch(() => identity(data)); | ||
| } | ||
|
|
||
| form( | ||
| formId: string, | ||
| interactionType: 'VIEWED' | 'SUBMITTED', | ||
| componentInstanceId: string | ||
| ): Promise<void> { | ||
| return this.dispatch(() => form(formId, interactionType, componentInstanceId)); | ||
| } | ||
|
|
||
| /** | ||
| * Ensure the SDK is initialized, then run `send`; swallow any failure at debug level. | ||
| * @param {() => Promise<unknown>} send - Events dispatch call to run once initialized. | ||
| */ | ||
| private async dispatch(send: () => Promise<unknown>): Promise<void> { | ||
| try { | ||
| const initialized = await this.ensureInit(); | ||
| if (!initialized) return; | ||
| await send(); | ||
| } catch (e) { | ||
| debug.common('analytics dispatch failed: %o', e); | ||
| } | ||
| } | ||
|
|
||
| private ensureInit(): Promise<boolean> { | ||
| if (!this.initPromise) { | ||
| this.initPromise = this.doInit(); | ||
| } | ||
| return this.initPromise; | ||
| } | ||
|
|
||
| private async doInit(): Promise<boolean> { | ||
| if (isDevMode()) { | ||
| debug.common('Browser Events SDK is not initialized in development environment'); | ||
| return false; | ||
| } | ||
| const edge = this.config?.api?.edge; | ||
| if (!edge?.clientContextId) { | ||
| debug.common('Client Edge API settings missing from configuration; analytics disabled'); | ||
| return false; | ||
| } | ||
| await initContentSdk({ | ||
| config: { | ||
| contextId: edge.clientContextId, | ||
| edgeUrl: edge.edgeUrl, | ||
| siteName: this.config?.defaultSite ?? '', | ||
| }, | ||
| plugins: [ | ||
| analyticsPlugin({ | ||
| options: { | ||
| enableCookie: true, | ||
| cookieDomain: normalizeCookieDomain(window.location.hostname), | ||
| }, | ||
| adapter: analyticsBrowserAdapter(), | ||
| }), | ||
| eventsPlugin(), | ||
| ], | ||
| }); | ||
| return true; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.