Skip to content

fix: running telemetry methods on initialized list only #1163

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
merged 2 commits into from
Sep 29, 2021
Merged
Changes from all 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
19 changes: 11 additions & 8 deletions projects/common/src/telemetry/user-telemetry-impl.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { UserTelemetryService } from './user-telemetry.service';

@Injectable({ providedIn: 'root' })
export class UserTelemetryImplService extends UserTelemetryService {
private telemetryProviders: UserTelemetryInternalConfig[] = [];
private allTelemetryProviders: UserTelemetryInternalConfig[] = [];
private initializedTelemetryProviders: UserTelemetryInternalConfig[] = [];

public constructor(private readonly injector: Injector, @Optional() private readonly router?: Router) {
super();
Expand All @@ -17,7 +18,7 @@ export class UserTelemetryImplService extends UserTelemetryService {
public register(...configs: UserTelemetryRegistrationConfig<unknown>[]): void {
try {
const providers = configs.map(config => this.buildTelemetryProvider(config));
this.telemetryProviders = [...this.telemetryProviders, ...providers];
this.allTelemetryProviders = [...this.allTelemetryProviders, ...providers];
} catch (error) {
/**
* Fail silently
Expand All @@ -29,31 +30,33 @@ export class UserTelemetryImplService extends UserTelemetryService {
}

public initialize(): void {
this.telemetryProviders.forEach(provider => provider.telemetryProvider.initialize(provider.initConfig));
this.allTelemetryProviders.forEach(provider => provider.telemetryProvider.initialize(provider.initConfig));
this.initializedTelemetryProviders = [...this.allTelemetryProviders];
Copy link
Contributor

Choose a reason for hiding this comment

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

what are we trying to solve here? were we calling before initializing? That's the only behavior that I see that would change here. Wouldn't that imply a misconfig though?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Currently, we don't 'initialize' telemetry when FF is disabled or when user is an internal user. But the track directive still pushes click events on the service. Our E2E tests fails when it sees any console error. In this case, the third party lib throws an error since we tried to send an 'event' without initializing the library.

Copy link
Contributor Author

@anandtiwary anandtiwary Sep 29, 2021

Choose a reason for hiding this comment

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

The other alternative is to put an init check in every method that will run every time the track event methods are called. We can avoid that by keeping a separate initialized list

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Still, a perf hit. But an ideal solution would be to load the entire module only if the feature flag is enabled.

}

public identify(userTraits: UserTraits): void {
this.telemetryProviders.forEach(provider => provider.telemetryProvider.identify(userTraits));
this.initializedTelemetryProviders.forEach(provider => provider.telemetryProvider.identify(userTraits));
}

public shutdown(): void {
this.telemetryProviders.forEach(provider => provider.telemetryProvider.shutdown?.());
this.initializedTelemetryProviders.forEach(provider => provider.telemetryProvider.shutdown?.());
this.initializedTelemetryProviders = [];
}

public trackEvent(name: string, data: Dictionary<unknown>): void {
this.telemetryProviders
this.initializedTelemetryProviders
.filter(provider => provider.enableEventTracking)
.forEach(provider => provider.telemetryProvider.trackEvent?.(name, data));
}

public trackPageEvent(url: string, data: Dictionary<unknown>): void {
this.telemetryProviders
this.initializedTelemetryProviders
.filter(provider => provider.enablePageTracking)
.forEach(provider => provider.telemetryProvider.trackPage?.(url, data));
}

public trackErrorEvent(error: string, data: Dictionary<unknown>): void {
this.telemetryProviders
this.initializedTelemetryProviders
.filter(provider => provider.enableErrorTracking)
.forEach(provider => provider.telemetryProvider.trackError?.(`Error: ${error}`, data));
}
Expand Down