Skip to content

Commit

Permalink
MOBILE-4368 analytics: Log searches
Browse files Browse the repository at this point in the history
  • Loading branch information
dpalou committed Jun 29, 2023
1 parent 0fff4a6 commit c7084f1
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 3 deletions.
31 changes: 31 additions & 0 deletions src/addons/mod/data/components/index/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import { AddonModDataModuleHandlerService } from '../../services/handlers/module
import { AddonModDataPrefetchHandler } from '../../services/handlers/prefetch';
import { AddonModDataComponentsCompileModule } from '../components-compile.module';
import { AddonModDataSearchComponent } from '../search/search';
import { CoreUrlUtils } from '@services/utils/url';
import { CoreTime } from '@singletons/time';

const contentToken = '<!-- CORE-DATABASE-CONTENT-GOES-HERE -->';

Expand Down Expand Up @@ -114,6 +116,7 @@ export class AddonModDataIndexComponent extends CoreCourseModuleMainActivityComp
protected entryChangedObserver?: CoreEventObserver;
protected ratingOfflineObserver?: CoreEventObserver;
protected ratingSyncObserver?: CoreEventObserver;
protected logSearch?: () => void;

constructor(
protected content?: IonContent,
Expand Down Expand Up @@ -404,6 +407,7 @@ export class AddonModDataIndexComponent extends CoreCourseModuleMainActivityComp
// Add data to search object.
if (modalData) {
this.search = modalData;
this.logSearch = CoreTime.once(() => this.performLogSearch());
this.searchEntries(0);
}
}
Expand All @@ -420,6 +424,8 @@ export class AddonModDataIndexComponent extends CoreCourseModuleMainActivityComp

try {
await this.fetchEntriesData();

this.logSearch?.();
} catch (error) {
CoreDomUtils.showErrorModalDefault(error, 'core.course.errorgetmodule', true);
} finally {
Expand Down Expand Up @@ -535,6 +541,31 @@ export class AddonModDataIndexComponent extends CoreCourseModuleMainActivityComp
this.analyticsLogEvent('mod_data_view_database');
}

/**
* Log search.
*/
protected async performLogSearch(): Promise<void> {
if (!this.database || !this.search.searching) {
return;
}

const params: Record<string, unknown> = {
perpage: AddonModDataProvider.PER_PAGE,
search: !this.search.searchingAdvanced ? this.search.text : '',
sort: this.search.sortBy,
order: this.search.sortDirection,
advanced: this.search.searchingAdvanced ? 1 : 0,
filter: 1,
};

// @todo: Add advanced search parameters. Leave them empty if not using advanced search.

this.analyticsLogEvent('mod_data_search_entries', {
data: params,
url: CoreUrlUtils.addParamsToUrl(`/mod/data/view.php?d=${this.database.id}`, params),
});
}

/**
* @inheritdoc
*/
Expand Down
18 changes: 18 additions & 0 deletions src/addons/mod/glossary/components/index/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import {
import { AddonModGlossaryModuleHandlerService } from '../../services/handlers/module';
import { AddonModGlossaryPrefetchHandler } from '../../services/handlers/prefetch';
import { AddonModGlossaryModePickerPopoverComponent } from '../mode-picker/mode-picker';
import { CoreTime } from '@singletons/time';

/**
* Component that displays a glossary entry page.
Expand Down Expand Up @@ -86,6 +87,7 @@ export class AddonModGlossaryIndexComponent extends CoreCourseModuleMainActivity
protected sourceUnsubscribe?: () => void;
protected observers?: CoreEventObserver[];
protected checkCompletionAfterLog = false; // Use CoreListItemsManager log system instead.
protected logSearch?: () => void;

getDivider?: (entry: AddonModGlossaryEntry) => string;
showDivider: (entry: AddonModGlossaryEntry, previous?: AddonModGlossaryEntry) => boolean = () => false;
Expand Down Expand Up @@ -226,6 +228,10 @@ export class AddonModGlossaryIndexComponent extends CoreCourseModuleMainActivity

this.hasOfflineRatings = hasOfflineRatings;
this.hasOffline = this.hasOfflineEntries || this.hasOfflineRatings;

if (this.isSearch && this.logSearch) {
this.logSearch();
}
}

/**
Expand Down Expand Up @@ -424,11 +430,23 @@ export class AddonModGlossaryIndexComponent extends CoreCourseModuleMainActivity
search(query: string): void {
this.loadingMessage = Translate.instant('core.searching');
this.showLoading = true;
this.logSearch = CoreTime.once(() => this.performLogSearch(query));

this.entries?.getSource().search(query);
this.loadContent();
}

/**
* Log search.
*
* @param query Text entered on the search box.
*/
protected async performLogSearch(query: string): Promise<void> {
this.analyticsLogEvent('mod_glossary_get_entries_by_search', {
data: { mode: 'search', hook: query, fullsearch: 1 },
});
}

/**
* @inheritdoc
*/
Expand Down
23 changes: 22 additions & 1 deletion src/core/features/courses/pages/list/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export class CoreCoursesListPage implements OnInit, OnDestroy {
protected courseIds = '';
protected isDestroyed = false;
protected logView: () => void;
protected logSearch?: () => void;

constructor() {
this.currentSiteId = CoreSites.getRequiredCurrentSite().getId();
Expand Down Expand Up @@ -159,7 +160,7 @@ export class CoreCoursesListPage implements OnInit, OnDestroy {
try {
if (this.searchMode) {
if (this.searchText) {
await this.search(this.searchText);
await this.searchCourses();
}
} else {
await this.loadCourses(true);
Expand Down Expand Up @@ -247,6 +248,7 @@ export class CoreCoursesListPage implements OnInit, OnDestroy {
this.courses = [];
this.searchPage = 0;
this.searchTotal = 0;
this.logSearch = CoreTime.once(() => this.performLogSearch());

const modal = await CoreDomUtils.showModalLoading('core.searching', true);
await this.searchCourses().finally(() => {
Expand All @@ -268,6 +270,23 @@ export class CoreCoursesListPage implements OnInit, OnDestroy {
this.fetchCourses();
}

/**
* Log search.
*/
protected async performLogSearch(): Promise<void> {
if (!this.searchMode) {
return;
}

CoreAnalytics.logEvent({
type: CoreAnalyticsEventType.VIEW_ITEM_LIST,
ws: 'core_course_search_courses',
name: Translate.instant('core.courses.availablecourses'),
data: { search: this.searchText, category: 'course' },
url: `/course/search.php?search=${this.searchText}`,
});
}

/**
* Load more courses.
*
Expand Down Expand Up @@ -305,6 +324,8 @@ export class CoreCoursesListPage implements OnInit, OnDestroy {

this.searchPage++;
this.canLoadMore = this.courses.length < this.searchTotal;

this.logSearch?.();
} catch (error) {
this.loadMoreError = true; // Set to prevent infinite calls with infinite-loading.
!this.isDestroyed && CoreDomUtils.showErrorModalDefault(error, 'core.courses.errorsearching', true);
Expand Down
2 changes: 1 addition & 1 deletion src/core/features/tag/pages/search/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ <h1>{{ 'core.tag.searchtags' | translate }}</h1>
autocorrect="off" [spellcheck]="false" [autoFocus]="false" [lengthCheck]="0" searchArea="CoreTag"></core-search-box>
</ion-col>
<ion-col class="ion-no-padding" size="12" size-sm="6" *ngIf="collections && collections.length > 1">
<core-combobox [selection]="collectionId" (onChange)="searchTags($event)" [disabled]="searching">
<core-combobox [selection]="collectionId" (onChange)="searchTags(query, $event)" [disabled]="searching">
<ion-select-option class="ion-text-wrap" [value]="0">
{{ 'core.tag.inalltagcoll' | translate }}
</ion-select-option>
Expand Down
28 changes: 27 additions & 1 deletion src/core/features/tag/pages/search/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export class CoreTagSearchPage implements OnInit {
searching = false;

protected logView: () => void;
protected logSearch?: () => void;

constructor() {
this.logView = CoreTime.once(async () => {
Expand Down Expand Up @@ -112,6 +113,8 @@ export class CoreTagSearchPage implements OnInit {
*/
async fetchTags(): Promise<void> {
this.cloud = await CoreTag.getTagCloud(this.collectionId, undefined, undefined, this.query);

this.logSearch?.();
}

/**
Expand Down Expand Up @@ -140,11 +143,17 @@ export class CoreTagSearchPage implements OnInit {
* Search tags.
*
* @param query Search query.
* @param collectionId Collection ID to use.
* @returns Resolved when done.
*/
searchTags(query: string): Promise<void> {
searchTags(query: string, collectionId?: number): Promise<void> {
this.searching = true;
this.query = query;
if (collectionId !== undefined) {
this.collectionId = collectionId;
}

this.logSearch = CoreTime.once(() => this.performLogSearch());
CoreApp.closeKeyboard();

return this.fetchTags().catch((error) => {
Expand All @@ -154,4 +163,21 @@ export class CoreTagSearchPage implements OnInit {
});
}

/**
* Log search.
*/
protected async performLogSearch(): Promise<void> {
if (!this.query) {
return;
}

CoreAnalytics.logEvent({
type: CoreAnalyticsEventType.VIEW_ITEM_LIST,
ws: 'core_tag_get_tag_cloud',
name: Translate.instant('core.tag.searchtags'),
data: { category: 'tag' },
url: `/tag/search.php&query=${this.query}&tc=${this.collectionId}&go=${Translate.instant('core.search')}`,
});
}

}

0 comments on commit c7084f1

Please sign in to comment.