Skip to content

Commit

Permalink
MOBILE-4368 analytics: Limit cases where setting is displayed
Browse files Browse the repository at this point in the history
Now the setting will only be displayed if there is an active handler
  • Loading branch information
dpalou committed Oct 31, 2023
1 parent 93b8ce2 commit 379156b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/core/features/settings/pages/general/general.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ <h1>{{ 'core.settings.general' | translate }}</h1>
</ion-label>
<ion-toggle [(ngModel)]="debugDisplay" (ionChange)="debugDisplayChanged($event)" slot="end"></ion-toggle>
</ion-item>
<ion-item class="ion-text-wrap" *ngIf="analyticsSupported">
<ion-item class="ion-text-wrap" *ngIf="analyticsAvailable">
<ion-label>
<p class="item-heading">{{ 'core.settings.enableanalytics' | translate }}</p>
<p>{{ 'core.settings.enableanalyticsdescription' | translate }}</p>
Expand Down
6 changes: 3 additions & 3 deletions src/core/features/settings/pages/general/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class CoreSettingsGeneralPage {
selectedZoomLevel = CoreZoomLevel.NONE;
richTextEditor = true;
debugDisplay = false;
analyticsSupported = false;
analyticsAvailable = false;
analyticsEnabled = false;
colorSchemes: CoreColorScheme[] = [];
selectedScheme: CoreColorScheme = CoreColorScheme.LIGHT;
Expand Down Expand Up @@ -101,8 +101,8 @@ export class CoreSettingsGeneralPage {

this.debugDisplay = await CoreConfig.get(CoreConstants.SETTINGS_DEBUG_DISPLAY, false);

this.analyticsSupported = CoreAnalytics.hasHandlers();
if (this.analyticsSupported) {
this.analyticsAvailable = await CoreAnalytics.isAnalyticsAvailable();
if (this.analyticsAvailable) {
this.analyticsEnabled = await CoreConfig.get(CoreConstants.SETTINGS_ANALYTICS_ENABLED, true);
}

Expand Down
28 changes: 28 additions & 0 deletions src/core/services/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,29 @@ export class CoreAnalyticsService extends CoreDelegate<CoreAnalyticsHandler> {
}
}

/**
* Check if analytics is available for the app/site.
*
* @returns True if available, false otherwise.
*/
async isAnalyticsAvailable(): Promise<boolean> {
if (Object.keys(this.enabledHandlers).length > 0) {
// There is an enabled handler, analytics is available.
return true;
}

// Check if there is a handler that is enabled at app level (enabled handlers are only set when logged in).
const enabledList = await Promise.all(Object.values(this.handlers).map(handler => {
if (!handler.appLevelEnabled) {
return false;
}

return handler.isEnabled();
}));

return enabledList.includes(true);
}

/**
* Log an event for the current site.
*
Expand Down Expand Up @@ -108,6 +131,11 @@ export const CoreAnalytics = makeSingleton(CoreAnalyticsService);
*/
export interface CoreAnalyticsHandler extends CoreDelegateHandler {

/**
* If true it means that the handler is enabled or not for the whole app, it doesn't depend on the site.
*/
appLevelEnabled?: boolean;

/**
* Log an event.
*
Expand Down

0 comments on commit 379156b

Please sign in to comment.