diff --git a/lib/__tests__/_sendEvent.test.ts b/lib/__tests__/_sendEvent.test.ts index 9caa77e3..a8fc1a15 100644 --- a/lib/__tests__/_sendEvent.test.ts +++ b/lib/__tests__/_sendEvent.test.ts @@ -389,7 +389,27 @@ describe("sendEvents", () => { expect(payload).toEqual({ events: [ expect.objectContaining({ - filters: ["brand:Apple"] + filters: ["brand%3AApple"] + }) + ] + }); + }); + + it("should uri-encodes filters", () => { + (analyticsInstance as any).sendEvents([ + { + eventType: "click", + eventName: "my-event", + index: "my-index", + filters: ["brand:Cool Brand"] + } + ]); + expect(XMLHttpRequest.send).toHaveBeenCalledTimes(1); + const payload = JSON.parse(XMLHttpRequest.send.mock.calls[0][0]); + expect(payload).toEqual({ + events: [ + expect.objectContaining({ + filters: ["brand%3ACool%20Brand"] }) ] }); diff --git a/lib/_sendEvent.ts b/lib/_sendEvent.ts index 72e8d6d2..ab187248 100644 --- a/lib/_sendEvent.ts +++ b/lib/_sendEvent.ts @@ -1,5 +1,6 @@ import { RequestFnType } from "./utils/request"; -import { InsightsEvent } from "./types"; +import { InsightsEvent } from './types' +import { isUndefined } from "./utils"; export function makeSendEvents(requestFn: RequestFnType) { return function sendEvents( @@ -14,10 +15,17 @@ export function makeSendEvents(requestFn: RequestFnType) { ); } - const events: InsightsEvent[] = eventData.map(data => ({ - ...data, - userToken: data?.userToken ?? this._userToken - })) + const events: InsightsEvent[] = eventData.map(data => { + const {filters, ...rest} = data; + const payload: InsightsEvent = { + ...rest, + userToken: data?.userToken ?? this._userToken + }; + if (!isUndefined(filters)) { + payload.filters = filters.map(encodeURIComponent) + } + return payload; + }) return sendRequest( requestFn, diff --git a/package.json b/package.json index b0ba3e23..a3993783 100644 --- a/package.json +++ b/package.json @@ -128,7 +128,7 @@ "bundlesize": [ { "path": "./dist/search-insights.min.js", - "maxSize": "2.4 kB" + "maxSize": "2.75 kB" } ] }