Skip to content
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

[angular][xmcloud] CDP page view component #1957

Merged
merged 16 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ Our versioning strategy is as follows:
* `[sitecore-jss]` GenericFieldValue model is updated to accept Date type ([#1916](https://github.com/Sitecore/jss/pull/1916))
* `[template/node-xmcloud-proxy]` `[sitecore-jss-proxy]` Introduced /api/healthz endpoint ([#1928](https://github.com/Sitecore/jss/pull/1928))
* `[sitecore-jss]` `[sitecore-jss-angular]` Render field metdata chromes in editMode metadata - in edit mode metadata in Pages, angular package field directives will render wrapping `code` elements with field metadata required for editing; ([#1926](https://github.com/Sitecore/jss/pull/1926))
* `[sitecore-jss-nextjs]` Expose MiddlewareBase class and MiddlewareBaseConfig type ([#1941](https://github.com/Sitecore/jss/pull/1941))
* `[angular-xmcloud]``[sitecore-jss-angular]` Analytics and CloudSDK integration
* `[angular-xmcloud]` Add CloudSDK initialization on client side ([#1952](https://github.com/Sitecore/jss/pull/1952))
* `[angular-xmcloud]` Add CloudSDK initialization on client side ([#1952](https://github.com/Sitecore/jss/pull/1952))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one requires migration guide as well. You can ping @art-alexeyenko to add it

* `[angular-xmcloud]``[sitecore-jss-angular]` Add CDP page view component to angular xmc app and add it to the app's scripts section. ([#1957](https://github.com/Sitecore/jss/pull/1957))
yavorsk marked this conversation as resolved.
Show resolved Hide resolved
illiakovalenko marked this conversation as resolved.
Show resolved Hide resolved


### 🛠 Breaking Change
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@ PROXY_HOST=http://localhost:3000

# Your XM Cloud Proxy server path is needed to build the app. The build output will be copied to the proxy server path.
PROXY_BUILD_PATH=<%- locals.relativeProxyAppDestination.replace(/\\/g, '\\\\') %>dist

# ==============================================

# An optional Sitecore Personalize scope identifier.
# This can be used to isolate personalization data when multiple XM Cloud Environments share a Personalize tenant.
# This should match the PAGES_PERSONALIZE_SCOPE environment variable for your connected XM Cloud Environment.
ANGULAR_PUBLIC_PERSONALIZE_SCOPE=
yavorsk marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { Component, OnInit, OnDestroy } from '@angular/core';
import { Subscription } from 'rxjs';
import { isServer, CdpHelper, LayoutServicePageState } from '@sitecore-jss/sitecore-jss-angular';
import { pageView, PageViewData } from '@sitecore-cloudsdk/events/browser';
import { JssContextService } from '../../jss-context.service';
import { JssState } from '../../JssState';
import { environment } from '../../../environments/environment';

/**
* This is the CDP page view component.
* It uses the Sitecore Cloud SDK to enable page view events on the client-side.
* See Sitecore Cloud SDK documentation for details.
* https://www.npmjs.com/package/@sitecore-cloudsdk/events
*/
@Component({
selector: 'app-cdp-page-view',
template: '',
})
export class CdpPageViewComponent implements OnInit, OnDestroy {
private contextSubscription: Subscription;

constructor(private jssContext: JssContextService) {}

ngOnInit(): void {
if (!isServer()) {
this.contextSubscription = this.jssContext.state.subscribe((newState: JssState) => {
const {
route,
context: { pageState, language, variantId },
} = newState.sitecore;

yavorsk marked this conversation as resolved.
Show resolved Hide resolved
// Do not create events in editing or preview mode or if missing route data
if (pageState !== LayoutServicePageState.Normal || !route?.itemId) {
return;
}

// Do not create events if disabled (e.g. we don't have consent)
if (this.disabled()) {
return;
}

const scope = process.env.ANGULAR_PUBLIC_PERSONALIZE_SCOPE;
const pageVariantId = CdpHelper.getPageVariantId(
route.itemId,
language || environment.defaultLanguage,
variantId as string,
scope
);

const pageViewData: PageViewData = {
channel: 'WEB',
currency: 'USD',
page: route.name,
pageVariantId,
language,
};

pageView(pageViewData).catch((err) => console.debug(err));
});
}
}

ngOnDestroy() {
if (this.contextSubscription) {
this.contextSubscription.unsubscribe();
}
}

/**
* Determines if the page view events should be turned off.
* IMPORTANT: You should implement based on your cookie consent management solution of choice.
* By default it is disabled if not in production mode
*/
disabled = () => {
return !environment.production;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class CloudSdkInitComponent implements OnInit {
constructor() {}

ngOnInit(): void {
if (!isServer) {
if (!isServer() && environment.production) {
CloudSDK({
siteName: environment.sitecoreSiteName,
sitecoreEdgeUrl: environment.sitecoreEdgeUrl,
Expand All @@ -25,8 +25,8 @@ export class CloudSdkInitComponent implements OnInit {
// Cookie may be created in personalize middleware (server), but if not we should create it here
enableBrowserCookie: true,
})
.addEvents()
.initialize();
.addEvents()
.initialize();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<ng-container>
<sc-editing-scripts></sc-editing-scripts>
<app-cloud-sdk-init></app-cloud-sdk-init>
<app-cdp-page-view></app-cdp-page-view>
</ng-container>
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { NgModule } from '@angular/core';
import { ScriptsComponent } from './scripts.component';
import { JssModule } from '@sitecore-jss/sitecore-jss-angular';
import { CloudSdkInitComponent } from './cloud-sdk-init.component';
import { CdpPageViewComponent } from './cdp-page-view.component';

@NgModule({
exports: [ScriptsComponent],
imports: [JssModule],
declarations: [ScriptsComponent, CloudSdkInitComponent],
declarations: [ScriptsComponent, CloudSdkInitComponent, CdpPageViewComponent],
})
export class ScriptsModule {}
2 changes: 2 additions & 0 deletions packages/sitecore-jss-angular/src/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export {
LayoutService,
LayoutServiceData,
LayoutServiceContextData,
LayoutServicePageState,
GraphQLLayoutService,
RestLayoutService,
PlaceholdersData,
Expand Down Expand Up @@ -101,3 +102,4 @@ export {
EventInstance,
PageViewInstance,
} from '@sitecore-jss/sitecore-jss/tracking';
export { CdpHelper } from '@sitecore-jss/sitecore-jss/personalize';