@@ -26,26 +26,28 @@ export class PageViewTracker extends BaseTracker {
2626 context : ClickstreamContext ;
2727 isEntrances = false ;
2828 searchKeywords = Event . Constants . KEYWORDS ;
29+ lastEngageTime = 0 ;
30+ lastScreenStartTimestamp = 0 ;
2931
3032 init ( ) {
3133 const configuredSearchKeywords = this . provider . configuration . searchKeyWords ;
3234 Object . assign ( this . searchKeywords , configuredSearchKeywords ) ;
33- this . trackPageView = this . trackPageView . bind ( this ) ;
35+ this . onPageChange = this . onPageChange . bind ( this ) ;
3436 if ( this . context . configuration . pageType === PageType . SPA ) {
3537 this . trackPageViewForSPA ( ) ;
3638 } else {
37- this . trackPageView ( ) ;
39+ this . onPageChange ( ) ;
3840 }
3941 }
4042
4143 trackPageViewForSPA ( ) {
42- MethodEmbed . add ( history , 'pushState' , this . trackPageView ) ;
43- MethodEmbed . add ( history , 'replaceState' , this . trackPageView ) ;
44- window . addEventListener ( 'popstate' , this . trackPageView ) ;
45- this . trackPageView ( ) ;
44+ MethodEmbed . add ( history , 'pushState' , this . onPageChange ) ;
45+ MethodEmbed . add ( history , 'replaceState' , this . onPageChange ) ;
46+ window . addEventListener ( 'popstate' , this . onPageChange ) ;
47+ this . onPageChange ( ) ;
4648 }
4749
48- trackPageView ( ) {
50+ onPageChange ( ) {
4951 if ( ! window . sessionStorage ) {
5052 logger . warn ( 'unsupported web environment for sessionStorage' ) ;
5153 return ;
@@ -55,41 +57,81 @@ export class PageViewTracker extends BaseTracker {
5557 const previousPageTitle = StorageUtil . getPreviousPageTitle ( ) ;
5658 const currentPageUrl = BrowserInfo . getCurrentPageUrl ( ) ;
5759 const currentPageTitle = BrowserInfo . getCurrentPageTitle ( ) ;
58- const previousPageStartTime = StorageUtil . getPreviousPageStartTime ( ) ;
59- let engagementTime = 0 ;
60- this . isEntrances =
61- this . provider . sessionTracker . session . isNewSession ( ) &&
62- previousPageUrl === '' ;
63- const currentPageStartTime = new Date ( ) . getTime ( ) ;
64- if ( previousPageStartTime > 0 ) {
65- engagementTime = currentPageStartTime - previousPageStartTime ;
66- }
67- if ( previousPageUrl !== currentPageUrl ) {
60+ if (
61+ previousPageUrl !== currentPageUrl ||
62+ previousPageTitle !== currentPageTitle
63+ ) {
6864 this . provider . scrollTracker ?. enterNewPage ( ) ;
69- const eventAttributes = {
70- [ Event . ReservedAttribute . PAGE_REFERRER ] : previousPageUrl ,
71- [ Event . ReservedAttribute . PAGE_REFERRER_TITLE ] : previousPageTitle ,
72- [ Event . ReservedAttribute . ENTRANCES ] : this . isEntrances ? 1 : 0 ,
73- } ;
74- if ( ! this . isEntrances ) {
75- eventAttributes [ Event . ReservedAttribute . ENGAGEMENT_TIMESTAMP ] =
76- engagementTime ;
77- }
78- this . provider . record ( {
79- name : Event . PresetEvent . PAGE_VIEW ,
80- attributes : eventAttributes ,
81- } ) ;
65+ this . recordUserEngagement ( ) ;
66+ this . trackPageView ( previousPageUrl , previousPageTitle ) ;
67+ this . trackSearchEvents ( ) ;
68+
8269 StorageUtil . savePreviousPageUrl ( currentPageUrl ) ;
8370 StorageUtil . savePreviousPageTitle ( currentPageTitle ) ;
84- StorageUtil . savePreviousPageStartTime ( currentPageStartTime ) ;
85- if ( this . context . configuration . isTrackSearchEvents ) {
86- this . trackSearchEvents ( ) ;
87- }
8871 }
8972 }
9073 }
9174
75+ trackPageView ( previousPageUrl : string , previousPageTitle : string ) {
76+ const previousPageStartTime = StorageUtil . getPreviousPageStartTime ( ) ;
77+ const analyticsEvent = this . provider . createEvent ( {
78+ name : Event . PresetEvent . PAGE_VIEW ,
79+ } ) ;
80+ const currentPageStartTime = analyticsEvent . timestamp ;
81+
82+ const eventAttributes = {
83+ [ Event . ReservedAttribute . PAGE_REFERRER ] : previousPageUrl ,
84+ [ Event . ReservedAttribute . PAGE_REFERRER_TITLE ] : previousPageTitle ,
85+ [ Event . ReservedAttribute . ENTRANCES ] : this . isEntrances ? 1 : 0 ,
86+ } ;
87+ if ( previousPageStartTime > 0 ) {
88+ eventAttributes [ Event . ReservedAttribute . PREVIOUS_TIMESTAMP ] =
89+ previousPageStartTime ;
90+ }
91+ if ( this . lastEngageTime > 0 ) {
92+ eventAttributes [ Event . ReservedAttribute . ENGAGEMENT_TIMESTAMP ] =
93+ this . lastEngageTime ;
94+ }
95+ Object . assign ( analyticsEvent . attributes , eventAttributes ) ;
96+ this . provider . recordEvent ( analyticsEvent ) ;
97+
98+ this . isEntrances = false ;
99+
100+ StorageUtil . savePreviousPageStartTime ( currentPageStartTime ) ;
101+ this . lastScreenStartTimestamp = currentPageStartTime ;
102+ }
103+
104+ setIsEntrances ( ) {
105+ this . isEntrances = true ;
106+ }
107+
108+ updateLastScreenStartTimestamp ( ) {
109+ this . lastScreenStartTimestamp = new Date ( ) . getTime ( ) ;
110+ }
111+
112+ recordUserEngagement ( isImmediate = false ) {
113+ if ( this . lastScreenStartTimestamp === 0 ) return ;
114+ this . lastEngageTime = this . getLastEngageTime ( ) ;
115+ if (
116+ this . provider . configuration . isTrackUserEngagementEvents &&
117+ this . lastEngageTime > Constants . minEngagementTime
118+ ) {
119+ this . provider . record ( {
120+ name : Event . PresetEvent . USER_ENGAGEMENT ,
121+ attributes : {
122+ [ Event . ReservedAttribute . ENGAGEMENT_TIMESTAMP ] : this . lastEngageTime ,
123+ } ,
124+ isImmediate : isImmediate ,
125+ } ) ;
126+ }
127+ }
128+
129+ getLastEngageTime ( ) {
130+ return new Date ( ) . getTime ( ) - this . lastScreenStartTimestamp ;
131+ }
132+
92133 trackSearchEvents ( ) {
134+ if ( ! this . context . configuration . isTrackSearchEvents ) return ;
93135 const searchStr = window . location . search ;
94136 if ( ! searchStr || searchStr . length === 0 ) return ;
95137 const urlParams = new URLSearchParams ( searchStr ) ;
@@ -108,3 +150,7 @@ export class PageViewTracker extends BaseTracker {
108150 }
109151 }
110152}
153+
154+ enum Constants {
155+ minEngagementTime = 1000 ,
156+ }
0 commit comments