Skip to content

Commit

Permalink
fix(headless): bump coveo.analytics to fix click tracking issue on io…
Browse files Browse the repository at this point in the history
  • Loading branch information
olamothe authored Aug 30, 2023
1 parent d25ba44 commit 402473f
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 39 deletions.
44 changes: 25 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 17 additions & 6 deletions packages/atomic/cypress/utils/analyticsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,23 @@ function isSearchEventRequest(
return 'results' in request;
}

export function parseClickEventRequest(
request: AnyEventRequest
): ClickEventRequest | null {
try {
return JSON.parse(
decodeURIComponent(request).replace('clickEvent=', '')
) as ClickEventRequest;
} catch (e) {
return null;
}
}

function isClickEventRequest(
request: AnyEventRequest
): request is ClickEventRequest {
return 'documentUri' in request;
): ClickEventRequest | null {
const parsed = parseClickEventRequest(request);
return parsed ? 'documentUri' in parsed : null;
}

function isCustomEventRequest(
Expand Down Expand Up @@ -66,10 +79,8 @@ export class AnalyticsTracker {
}

static getLastClickEvent() {
return findLast(
this.analytics,
isClickEventRequest
) as ClickEventRequest | null;
const last = findLast(this.analytics, isClickEventRequest);
return parseClickEventRequest(last);
}

static getLastCustomEvent(eventType: string) {
Expand Down
2 changes: 1 addition & 1 deletion packages/headless/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"@types/pino": "6.3.12",
"@types/redux-mock-store": "1.0.3",
"abab": "2.0.6",
"coveo.analytics": "2.28.12",
"coveo.analytics": "2.28.14",
"cross-fetch": "3.1.5",
"dayjs": "1.11.5",
"exponential-backoff": "3.1.0",
Expand Down
27 changes: 26 additions & 1 deletion packages/quantic/cypress/e2e/common-expectations.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// eslint-disable-next-line node/no-unpublished-import
import {CyHttpMessages, Interception} from 'cypress/types/net-stubbing';
import {ComponentErrorSelector, should} from './common-selectors';

export function logUaEvent(
Expand All @@ -9,7 +11,7 @@ export function logUaEvent(
return cy
.wait(requestAlias)
.then((interception) => {
const analyticsBody = interception.request.body;
const analyticsBody = getAnalyticsBodyFromInterception(interception);

Object.keys(bodyData).forEach((key: string) => {
expect(analyticsBody).to.have.property(key, bodyData[key]);
Expand Down Expand Up @@ -46,3 +48,26 @@ export function ComponentErrorExpectations(selector: ComponentErrorSelector) {
},
};
}

export function getAnalyticsBodyFromInterception(interception: Interception) {
return getAnalyticsBodyFromRequest(interception.request);
}

export function getAnalyticsBodyFromRequest(
req: CyHttpMessages.IncomingRequest
) {
// Some analytics request won't be application/json, but will be form-url-encoded (specifically, click events).
// Need to decode them if that's the case

// eslint-disable-next-line @typescript-eslint/no-explicit-any
let analyticsBody: Record<string, any>;
if (typeof req.body === 'string') {
analyticsBody = JSON.parse(
decodeURIComponent(req.body).replace('clickEvent=', '')
);
} else {
analyticsBody = req.body;
}

return analyticsBody;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import {Interception} from '../../../../../../node_modules/cypress/types/net-stubbing';
import {InterceptAliases} from '../../../page-objects/search';
import {ComponentErrorExpectations} from '../../common-expectations';
import {
ComponentErrorExpectations,
getAnalyticsBodyFromInterception,
} from '../../common-expectations';
import {should} from '../../common-selectors';
import {ConsoleExpectations} from '../../console-expectations';
import {EventExpectations} from '../../event-expectations';
Expand Down Expand Up @@ -130,7 +133,7 @@ export function recommendationListExpectations(
logRecommendationOpen: (index: number, recommendationsAlias: string) => {
cy.wait(InterceptAliases.UA.RecommendationOpen)
.then((interception) => {
const analyticsBody = interception.request.body;
const analyticsBody = getAnalyticsBodyFromInterception(interception);
const customData = analyticsBody?.customData;
cy.get<Array<Result>>(recommendationsAlias).then(
(recommendations) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {getQueryAlias, InterceptAliases} from '../../../../page-objects/search';
import {getAnalyticsBodyFromInterception} from '../../../common-expectations';
import {should} from '../../../common-selectors';
import {EventExpectations} from '../../../event-expectations';
import {resultListExpectations} from '../result-list-expectations';
Expand Down Expand Up @@ -30,8 +31,8 @@ function logFoldingActionEvent(
const {title, uri, position, documentId} = result || {};
cy.wait(eventName)
.then((interception) => {
const analyticsBody = interception.request.body;
const customData = analyticsBody?.customData;
const analyticsBody = getAnalyticsBodyFromInterception(interception);
const customData = analyticsBody.customData;
if (eventType === 'click') {
expect(analyticsBody).to.have.property('documentTitle', title);
expect(analyticsBody).to.have.property('documentUri', uri);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {InterceptAliases} from '../../../page-objects/search';
import {getAnalyticsBodyFromInterception} from '../../common-expectations';
import {should} from '../../common-selectors';
import {EventExpectations} from '../../event-expectations';
import {
Expand Down Expand Up @@ -78,7 +79,7 @@ function resultQuickviewExpectations(selector: ResultQuickviewSelector) {
logDocumentQuickview: (title: string) => {
cy.wait(InterceptAliases.UA.DocumentQuickview)
.then((interception) => {
const analyticsBody = interception.request.body;
const analyticsBody = getAnalyticsBodyFromInterception(interception);
expect(analyticsBody).to.have.property(
'actionCause',
'documentQuickview'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {InterceptAliases} from '../../../page-objects/search';
import {getAnalyticsBodyFromInterception} from '../../common-expectations';
import {should} from '../../common-selectors';
import {
SmartSnippetSuggestionsSelector,
Expand Down Expand Up @@ -33,7 +34,7 @@ function logSmartSnippetSuggestionsEvent(
} = suggestion;
cy.wait(eventName)
.then((interception) => {
const analyticsBody = interception.request.body;
const analyticsBody = getAnalyticsBodyFromInterception(interception);
const customData = analyticsBody?.customData;
const documentIdPayload = customData?.documentId;
expect(customData).to.have.property('answerSnippet', answerSnippet);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {InterceptAliases} from '../../../page-objects/search';
import {getAnalyticsBodyFromInterception} from '../../common-expectations';
import {should} from '../../common-selectors';
import {
SmartSnippetSelector,
Expand Down Expand Up @@ -130,7 +131,7 @@ function smartSnippetExpectations(selector: SmartSnippetSelector) {
const {title, uri, permanentId} = document;
cy.wait(InterceptAliases.UA.OpenSmartSnippetSource)
.then((interception) => {
const analyticsBody = interception.request.body;
const analyticsBody = getAnalyticsBodyFromInterception(interception);
const customData = analyticsBody?.customData;
expect(analyticsBody).to.have.property('documentTitle', title);
expect(analyticsBody).to.have.property('documentUri', uri);
Expand All @@ -152,7 +153,7 @@ function smartSnippetExpectations(selector: SmartSnippetSelector) {
const {linkUrl, linkText, title, uri, permanentId} = document;
cy.wait(InterceptAliases.UA.OpenSmartSnippetInlineLink)
.then((interception) => {
const analyticsBody = interception.request.body;
const analyticsBody = getAnalyticsBodyFromInterception(interception);
const customData = analyticsBody?.customData;
expect(analyticsBody).to.have.property('documentTitle', title);
expect(analyticsBody).to.have.property('documentUri', uri);
Expand Down Expand Up @@ -192,7 +193,7 @@ function smartSnippetExpectations(selector: SmartSnippetSelector) {
}) => {
cy.wait(InterceptAliases.UA.SendSmartSnippetReason)
.then((interception) => {
const analyticsBody = interception.request.body;
const analyticsBody = getAnalyticsBodyFromInterception(interception);
const customData = analyticsBody?.customData;
expect(analyticsBody).to.have.property('eventType', 'smartSnippet');
expect(customData).to.have.property('reason', payload.reason);
Expand Down
9 changes: 6 additions & 3 deletions packages/quantic/cypress/page-objects/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
RouteMatcher,
StaticResponse, // eslint-disable-next-line node/no-unpublished-import
} from 'cypress/types/net-stubbing';
import {getAnalyticsBodyFromRequest} from '../e2e/common-expectations';
import {useCaseEnum} from './use-case';

type RequestParams = Record<string, string | number | boolean | undefined>;
Expand Down Expand Up @@ -94,10 +95,12 @@ export const routeMatchers = {
export function interceptSearch() {
return cy
.intercept('POST', routeMatchers.analytics, (req) => {
if (req.body.actionCause) {
req.alias = uaAlias(req.body.actionCause).substring(1);
const analyticsBody = getAnalyticsBodyFromRequest(req);

if (analyticsBody.actionCause) {
req.alias = uaAlias(analyticsBody.actionCause as string).substring(1);
} else if (req.body.eventType) {
req.alias = uaAlias(req.body.eventValue).substring(1);
req.alias = uaAlias(analyticsBody.eventValue as string).substring(1);
}
})

Expand Down

0 comments on commit 402473f

Please sign in to comment.