Browse Source

feat(event): Added event to track search inputs #1553

Kevin Mattutat 3 years ago
parent
commit
f19406753f

+ 22 - 0
packages/core/src/event-bus/events/search-event.ts

@@ -0,0 +1,22 @@
+import { SearchInput } from '@vendure/common/lib/generated-types';
+
+import { RequestContext } from '../../api/common/request-context';
+import { VendureEvent } from '../vendure-event';
+
+type ExtendedSearchInput = SearchInput & {
+    [extendedInputField: string]: any;
+};
+
+/**
+ * @description
+ * This event is fired whenever a search query is executed.
+ *
+ * @docsCategory events
+ * @docsPage Event Types
+ * @since 1.5.3
+ */
+export class SearchEvent extends VendureEvent {
+    constructor(public ctx: RequestContext, public input: ExtendedSearchInput) {
+        super();
+    }
+}

+ 3 - 0
packages/core/src/plugin/default-search-plugin/fulltext-search.service.ts

@@ -7,6 +7,7 @@ import { InternalServerError } from '../../common/error/errors';
 import { TransactionalConnection } from '../../connection/transactional-connection';
 import { Collection, FacetValue } from '../../entity';
 import { EventBus } from '../../event-bus/event-bus';
+import { SearchEvent } from '../../event-bus/events/search-event';
 import { Job } from '../../job-queue/job';
 import { CollectionService } from '../../service/services/collection.service';
 import { FacetValueService } from '../../service/services/facet-value.service';
@@ -54,6 +55,8 @@ export class FulltextSearchService {
     ): Promise<Omit<Omit<SearchResponse, 'facetValues'>, 'collections'>> {
         const items = await this._searchStrategy.getSearchResults(ctx, input, enabledOnly);
         const totalItems = await this._searchStrategy.getTotalCount(ctx, input, enabledOnly);
+        this.eventBus.publish(new SearchEvent(ctx, input));
+
         return {
             items,
             totalItems,

+ 5 - 0
packages/elasticsearch-plugin/src/elasticsearch.service.ts

@@ -6,12 +6,14 @@ import {
     CollectionService,
     ConfigService,
     DeepRequired,
+    EventBus,
     FacetValue,
     FacetValueService,
     InternalServerError,
     Job,
     Logger,
     RequestContext,
+    SearchEvent,
     SearchService,
 } from '@vendure/core';
 import equal from 'fast-deep-equal/es6';
@@ -46,6 +48,7 @@ export class ElasticsearchService implements OnModuleInit, OnModuleDestroy {
         private configService: ConfigService,
         private facetValueService: FacetValueService,
         private collectionService: CollectionService,
+        private eventBus: EventBus,
     ) {
         searchService.adopt(this);
     }
@@ -191,6 +194,7 @@ export class ElasticsearchService implements OnModuleInit, OnModuleDestroy {
                     body: elasticSearchBody,
                 });
                 const totalItems = await this.totalHits(ctx, input, groupByProduct);
+                this.eventBus.publish(new SearchEvent(ctx, input));
                 return {
                     items: body.hits.hits.map(hit => this.mapProductToSearchResult(hit)),
                     totalItems,
@@ -215,6 +219,7 @@ export class ElasticsearchService implements OnModuleInit, OnModuleDestroy {
                     index: indexPrefix + VARIANT_INDEX_NAME,
                     body: elasticSearchBody,
                 });
+                this.eventBus.publish(new SearchEvent(ctx, input));
                 return {
                     items: body.hits.hits.map(hit => this.mapVariantToSearchResult(hit)),
                     totalItems: body.hits.total ? body.hits.total.value : 0,