add support for search category
Signed-off-by: Nikita Karpukhin <nikita.karpukhin@scout24.com>
This commit is contained in:
+7
-5
@@ -25,6 +25,7 @@ describe('GoogleAnalytics', () => {
|
||||
pluginId: 'some-plugin',
|
||||
routeRef: 'unknown',
|
||||
releaseNum: 1337,
|
||||
searchTypes: 'test category',
|
||||
};
|
||||
const trackingId = 'UA-000000-0';
|
||||
const basicValidConfig = new ConfigReader({
|
||||
@@ -175,7 +176,7 @@ describe('GoogleAnalytics', () => {
|
||||
expect(command).toBe('send');
|
||||
expect(data).toMatchObject({
|
||||
hitType: 'pageview',
|
||||
page: '/search?query=test search',
|
||||
page: '/search?query=test+search',
|
||||
});
|
||||
expect(ReactGA.testModeAPI.calls).toHaveLength(2);
|
||||
});
|
||||
@@ -203,7 +204,7 @@ describe('GoogleAnalytics', () => {
|
||||
expect(pageviewCommand).toBe('send');
|
||||
expect(pageViewData).toMatchObject({
|
||||
hitType: 'pageview',
|
||||
page: '/search?query=test search',
|
||||
page: '/search?query=test+search',
|
||||
});
|
||||
const [searchCommand, searchData] = ReactGA.testModeAPI.calls[2];
|
||||
expect(searchCommand).toBe('send');
|
||||
@@ -215,7 +216,7 @@ describe('GoogleAnalytics', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('captures virtual pageviews on custom route with custom query param', () => {
|
||||
it('captures virtual pageviews on custom route with custom search query and custom category', () => {
|
||||
const config = new ConfigReader({
|
||||
app: {
|
||||
analytics: {
|
||||
@@ -225,7 +226,8 @@ describe('GoogleAnalytics', () => {
|
||||
virtualSearchPageView: {
|
||||
mode: 'only',
|
||||
mountPath: '/custom',
|
||||
queryParam: 'term',
|
||||
searchQuery: 'term',
|
||||
categoryQuery: 'sc',
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -242,7 +244,7 @@ describe('GoogleAnalytics', () => {
|
||||
expect(command).toBe('send');
|
||||
expect(data).toMatchObject({
|
||||
hitType: 'pageview',
|
||||
page: '/custom?term=test search',
|
||||
page: '/custom?term=test+search&sc=test+category',
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
+8
-2
@@ -167,9 +167,15 @@ export class GoogleAnalytics implements AnalyticsApi {
|
||||
}
|
||||
|
||||
if (this.virtualSearchPageView.mode !== 'disabled' && action === 'search') {
|
||||
const { mountPath, queryParam } = this.virtualSearchPageView;
|
||||
const { mountPath, searchQuery, categoryQuery } =
|
||||
this.virtualSearchPageView;
|
||||
const params = new URLSearchParams();
|
||||
params.set(searchQuery, subject);
|
||||
if (categoryQuery) {
|
||||
params.set(categoryQuery, context.searchTypes?.toString() ?? '');
|
||||
}
|
||||
this.capture.pageview(
|
||||
`${mountPath}?${queryParam}=${subject}`,
|
||||
`${mountPath}?${params.toString()}`,
|
||||
customMetadata,
|
||||
);
|
||||
if (this.virtualSearchPageView.mode === 'only') {
|
||||
|
||||
@@ -20,7 +20,8 @@ type VirtualSearchPageViewType = 'disabled' | 'only' | 'both';
|
||||
export type VirtualSearchPageViewConfig = {
|
||||
mode: VirtualSearchPageViewType;
|
||||
mountPath: string;
|
||||
queryParam: string;
|
||||
searchQuery: string;
|
||||
categoryQuery?: string;
|
||||
};
|
||||
|
||||
function isVirtualSearchPageViewType(
|
||||
@@ -38,6 +39,7 @@ export function parseVirtualSearchPageViewConfig(
|
||||
? vspvModeString
|
||||
: 'disabled',
|
||||
mountPath: config?.getOptionalString('mountPath') ?? '/search',
|
||||
queryParam: config?.getOptionalString('queryParam') ?? 'query',
|
||||
searchQuery: config?.getOptionalString('searchQuery') ?? 'query',
|
||||
categoryQuery: config?.getOptionalString('categoryQuery'),
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user