-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add service support to analytics mechnism
- Loading branch information
Showing
58 changed files
with
810 additions
and
66 deletions.
There are no files selected for viewing
This file contains 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
2 changes: 1 addition & 1 deletion
2
...@o3r/analytics/schematics/analytics-to-component/templates/__name__.analytics.ts.template
This file contains 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
58 changes: 58 additions & 0 deletions
58
packages/@o3r/analytics/schematics/analytics-to-component/templates/ttt.ts
This file contains 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,58 @@ | ||
import type { EventInfo, AnalyticsEvent, Attribute, ConstructorAnalyticsEvent, ConstructorAnalyticsEventParameters, AnalyticsEvents} from '@o3r/analytics'; | ||
|
||
const dummyEvent = () => { | ||
|
||
} | ||
|
||
/** | ||
* Dummy event to show how we can use analytics event | ||
*/ | ||
export class DummyEvent implements AnalyticsEvent { | ||
/** @inheritdoc */ | ||
public eventInfo: EventInfo = { | ||
eventName: 'DummyEvent' | ||
}; | ||
} | ||
|
||
/** | ||
* Interface to define the inputs that RuntimeDummyEvent needs to be created | ||
*/ | ||
export interface RuntimeDummyEventConstructorAnalyticsEventParameters extends ConstructorAnalyticsEventParameters { | ||
/** | ||
* Example of runtime data | ||
*/ | ||
runtimeData?: string; | ||
} | ||
|
||
/** | ||
* Dummy event with runtime data to show how we can use analytics event | ||
*/ | ||
export class RuntimeDummyEvent implements AnalyticsEvent { | ||
/** @inheritdoc */ | ||
public eventInfo: EventInfo = { | ||
eventName: 'RuntimeDummyEvent' | ||
}; | ||
|
||
/** @inheritdoc */ | ||
public attributes: Attribute[] | ||
|
||
constructor(parameters?: RuntimeDummyEventConstructorAnalyticsEventParameters) { | ||
this.attributes = parameters?.runtimeData ? [{ key: 'runtimeData', value: parameters.runtimeData }] : []; | ||
} | ||
} | ||
|
||
/** | ||
* Interface for the analytics of <%= componentName %> | ||
*/ | ||
export interface ComponentAnalytics extends AnalyticsEvents { | ||
dummyEvent: ConstructorAnalyticsEvent<DummyEvent>; | ||
runtimeDummyEvent: ConstructorAnalyticsEvent<RuntimeDummyEvent>; | ||
} | ||
|
||
/** | ||
* Definition of the analytics of <%= componentName %> | ||
*/ | ||
export const analyticsEvents: ComponentAnalytics = { | ||
dummyEvent: DummyEvent, | ||
runtimeDummyEvent: RuntimeDummyEvent | ||
}; |
This file contains 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains 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
File renamed without changes.
This file contains 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
File renamed without changes.
File renamed without changes.
This file contains 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
File renamed without changes.
This file contains 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,4 @@ | ||
export * from './contracts/index'; | ||
export * from './directives'; | ||
export * from './services/index'; | ||
export * from './stores/index'; |
This file contains 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
File renamed without changes.
This file contains 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains 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 |
---|---|---|
@@ -1,5 +1,2 @@ | ||
export * from './contracts/index'; | ||
export * from './directives/index'; | ||
export * from './services/index'; | ||
export * from './stores/index'; | ||
|
||
export * from './performance/index'; | ||
export * from './tracker/index'; |
22 changes: 22 additions & 0 deletions
22
packages/@o3r/analytics/src/tracker/directives/click-event.directive.ts
This file contains 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,22 @@ | ||
import { Directive, input } from '@angular/core'; | ||
import { AnalyticTrackGeneric } from './generic-event.directive'; | ||
|
||
@Directive({ | ||
selector: '[trackClick]', | ||
standalone: true | ||
}) | ||
/** Directive to listen and emit analytics in case of Dom Click event */ | ||
export class AnalyticTrackClick extends AnalyticTrackGeneric { | ||
/** @inheritdoc */ | ||
public trackEvent = input(undefined, { alias: 'trackClick' }); | ||
/** @inheritdoc */ | ||
public trackCategory = input('', { alias: 'trackClickCategory' }); | ||
/** @inheritdoc */ | ||
public trackAction = input(undefined, { alias: 'trackClickAction' }); | ||
/** @inheritdoc */ | ||
public trackLabel = input(undefined, { alias: 'trackClickLabel' }); | ||
/** @inheritdoc */ | ||
public trackValue = input(undefined, { alias: 'trackClickValue' }); | ||
/** @inheritdoc */ | ||
public readonly eventName = 'click'; | ||
} |
82 changes: 82 additions & 0 deletions
82
packages/@o3r/analytics/src/tracker/directives/events.directive.ts
This file contains 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,82 @@ | ||
import { Directive, effect, ElementRef, inject, input,type OnInit, Renderer2 } from '@angular/core'; | ||
import { AnalyticsEventReporter } from '../services/tracker/analytics-reporter.service'; | ||
import type { AnalyticsWellKnownDomActionType } from '../events'; | ||
|
||
type TrackEventName = keyof GlobalEventHandlersEventMap; | ||
|
||
@Directive({ | ||
selector: '[trackEvents]', | ||
standalone: true | ||
}) | ||
export class AnalyticTrackEvent implements OnInit { | ||
/** List the Dom Element events to listen and for which emitting analytics event. */ | ||
public trackEvents = input.required<TrackEventName[]>(); | ||
/** Category of the events */ | ||
public trackCategory = input<string>(''); | ||
/** | ||
* Name of the action as defined in analytics service. | ||
* The name of the Dom Event will be used if not specified. | ||
*/ | ||
public trackAction = input<AnalyticsWellKnownDomActionType | undefined>(); | ||
/** Label of the events */ | ||
public trackLabel = input<string | undefined>(); | ||
/** Value of the events */ | ||
public trackValue = input<any>(); | ||
|
||
protected readonly el = inject(ElementRef); | ||
protected readonly trackEventsService = inject(AnalyticsEventReporter); | ||
protected readonly renderer = inject(Renderer2); | ||
protected readonly isTrackingActive; | ||
protected listeningEvents: {[x in TrackEventName]?: () => void} = {}; | ||
|
||
constructor() { | ||
this.isTrackingActive = this.trackEventsService.isTrackingActive; | ||
} | ||
|
||
/** | ||
* Create the listener for the given event | ||
* @param eventName name of the event to listen | ||
*/ | ||
protected nativeListen(eventName: TrackEventName) { | ||
// Renderer is used because it is manipulating the DOM and when the element is destroyed the event listener is destroyed too. | ||
// Usage of an observable from event was not possible because the ngOnDestroy with the unsubscribe was called before the ui event was handled | ||
return this.renderer.listen(this.el.nativeElement, eventName, (event) => { | ||
const action = this.trackAction() || eventName as AnalyticsWellKnownDomActionType; | ||
this.trackEventsService.reportEvent({ | ||
type: 'event', | ||
action, | ||
category: this.trackCategory(), | ||
label: this.trackLabel(), | ||
event, | ||
attributes: this.trackValue() || this.el.nativeElement?.value | ||
}); | ||
}); | ||
} | ||
|
||
/** Remove the created events listeners */ | ||
protected unlisten() { | ||
Object.values(this.listeningEvents).forEach((fn) => fn); | ||
this.listeningEvents = {}; | ||
} | ||
|
||
public ngOnInit(): void { | ||
effect(() => { | ||
const analyticEvent = this.trackEvents(); | ||
if (this.isTrackingActive()) { | ||
this.listeningEvents = { | ||
...this.listeningEvents, | ||
...Object.fromEntries(analyticEvent | ||
.filter((e) => !this.listeningEvents[e]) | ||
.map((e) => [e, this.nativeListen(e)]) | ||
) | ||
}; | ||
} | ||
}); | ||
|
||
effect(() => { | ||
if (!this.isTrackingActive()) { | ||
this.unlisten(); | ||
} | ||
}); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
packages/@o3r/analytics/src/tracker/directives/focus-event.directive.ts
This file contains 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,22 @@ | ||
import { Directive, input } from '@angular/core'; | ||
import { AnalyticTrackGeneric } from './generic-event.directive'; | ||
|
||
@Directive({ | ||
selector: '[trackFocus]', | ||
standalone: true | ||
}) | ||
/** Directive to listen and emit analytics in case of Dom Focus event */ | ||
export class AnalyticTrackFocus extends AnalyticTrackGeneric { | ||
/** @inheritdoc */ | ||
public trackEvent = input(undefined, { alias: 'trackFocus' }); | ||
/** @inheritdoc */ | ||
public trackCategory = input('', { alias: 'trackFocusCategory' }); | ||
/** @inheritdoc */ | ||
public trackAction = input(undefined, { alias: 'trackFocusAction' }); | ||
/** @inheritdoc */ | ||
public trackLabel = input(undefined, { alias: 'trackFocusLabel' }); | ||
/** @inheritdoc */ | ||
public trackValue = input(undefined, { alias: 'trackFocusValue' }); | ||
/** @inheritdoc */ | ||
public readonly eventName = 'focus'; | ||
} |
Oops, something went wrong.