diff --git a/CHANGELOG.md b/CHANGELOG.md index 363340a80..b44686639 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog + +## 1.7.2 + +- Fix autoPageDetection command error caused by closure-compiler. + ## 1.7.1 - Auto detect page transitions on route change. diff --git a/lib/commands.ts b/lib/commands.ts index 8c0ab7338..49fdfa8f4 100644 --- a/lib/commands.ts +++ b/lib/commands.ts @@ -5,6 +5,7 @@ import {hasOwnProperty} from './util'; import {setPage} from './pageChange'; import {warn} from './debug'; import vars from './vars'; +import {processAutoPageDetectionCommand} from './hooks/autoPageDetection'; export function processCommand(command: any[]): any { switch (command[0]) { @@ -64,7 +65,7 @@ export function processCommand(command: any[]): any { vars.wrapEventHandlers = command[1]; break; case 'autoPageDetection': - vars.autoPageDetection = command[1]; + vars.autoPageDetection = processAutoPageDetectionCommand(command[1]); break; case 'wrapTimers': vars.wrapTimers = command[1]; diff --git a/lib/hooks/autoPageDetection.ts b/lib/hooks/autoPageDetection.ts index 9ef742063..3a1dbcb44 100644 --- a/lib/hooks/autoPageDetection.ts +++ b/lib/hooks/autoPageDetection.ts @@ -1,5 +1,5 @@ import {win, doc} from '../browser'; -import type {MappingRule} from '../types'; +import type {AutoPageDetectionType, MappingRule} from '../types'; import vars from '../vars'; import {info, debug, error} from '../debug'; import {setPage} from '../pageChange'; @@ -20,7 +20,7 @@ function setupAutoPageDetection() { win.addEventListener('hashchange', function (event) { if (DEBUG) { - info(`hashchange to ${event.newURL} from ${event.newURL}, current location ${win.location}`); + info(`hashchange to ${event.newURL} from ${event.oldURL}, current location ${win.location}`); } handlePossibleUrlChange(event.newURL); }); @@ -115,7 +115,7 @@ function handlePossibleUrlChange(newUrl: string) { function applyCustomPageMappings(urlPath: string): string | null { const rules = getAutoPageDetectionMappingRule(); - const effectivePath = (titleAsPageNameInAutoPageDetectio() ? doc.title : urlPath) || urlPath; + const effectivePath = (titleAsPageNameInAutoPageDetection() ? doc.title : urlPath) || urlPath; if (!effectivePath || !rules.length) { return effectivePath; } @@ -149,11 +149,11 @@ export function isAutoPageDetectionEnabled(): boolean { return !!vars.autoPageDetection; } -function ignorePopstateEvent(): boolean { +export function ignorePopstateEvent(): boolean { return typeof vars.autoPageDetection === 'object' && !!vars.autoPageDetection?.ignorePopstateEvent; } -function titleAsPageNameInAutoPageDetectio(): boolean { +export function titleAsPageNameInAutoPageDetection(): boolean { return typeof vars.autoPageDetection === 'object' && !!vars.autoPageDetection?.titleAsPageName; } @@ -163,3 +163,20 @@ function getAutoPageDetectionMappingRule(): Array { } return vars.autoPageDetection.mappingRule; } + +export function processAutoPageDetectionCommand(input: any): boolean | AutoPageDetectionType { + const guessCmd = input as boolean | AutoPageDetectionType | null; + if (!guessCmd) { + return false; + } + + if (typeof guessCmd !== 'object') { + return !!guessCmd; + } + + return { + ignorePopstateEvent: guessCmd['ignorePopstateEvent'], + titleAsPageName: guessCmd['titleAsPageName'], + mappingRule: guessCmd['mappingRule'] + }; +} diff --git a/package.json b/package.json index 1b3428c3d..1c9f75080 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "version": "1.7.1", + "version": "1.7.2", "name": "@instana/weasel", "description": "Collect end-user data", "main": "lib/index.ts", diff --git a/test/unit/hooks/autoPageDetection.test.ts b/test/unit/hooks/autoPageDetection.test.ts index d228fba9b..cd1c53b79 100644 --- a/test/unit/hooks/autoPageDetection.test.ts +++ b/test/unit/hooks/autoPageDetection.test.ts @@ -2,10 +2,12 @@ import {expect} from 'chai'; import {win} from '@lib/browser'; import defaultVars from '@lib/vars'; -import type {Beacon} from '@lib/types'; +import vars from '@lib/vars'; +import type {Beacon, AutoPageDetectionType} from '@lib/types'; import {stripSecrets} from '@lib/stripSecrets'; import {addCommonBeaconProperties} from '@lib/commonBeaconProperties'; import {initAutoPageDetection, isAutoPageDetectionEnabled} from '@lib/hooks/autoPageDetection'; +import {ignorePopstateEvent, titleAsPageNameInAutoPageDetection, processAutoPageDetectionCommand} from '@lib/hooks/autoPageDetection'; describe('autodetection of page transition', () => { @@ -17,6 +19,10 @@ describe('autodetection of page transition', () => { const result: boolean = isAutoPageDetectionEnabled(); addCommonBeaconProperties(beacon); expect(result).equal(false); + + const titleResult: boolean = titleAsPageNameInAutoPageDetection(); + expect(titleResult).equal(false); + expect(beacon.uf).not.exist; }); @@ -36,11 +42,65 @@ describe('autodetection of page transition', () => { }; const result: boolean = isAutoPageDetectionEnabled(); expect(result).equal(true); + + const titleResult: boolean = titleAsPageNameInAutoPageDetection(); + expect(titleResult).equal(true); + const beacon: Partial = {}; addCommonBeaconProperties(beacon); expect(beacon.uf).equal('sn'); }); }); + + describe('autoPageDetection command test', () => { + it('autoPageDetection command parsing normal', () => { + const cmd: any = {mappingRule: [[/.*section*/, 'Section 1']], ignorePopstateEvent: false, titleAsPageName: true}; + vars.autoPageDetection = processAutoPageDetectionCommand(cmd); + expect(isAutoPageDetectionEnabled()).equal(true); + expect(ignorePopstateEvent()).equal(false); + expect(titleAsPageNameInAutoPageDetection()).equal(true); + const resultAPD = vars.autoPageDetection as AutoPageDetectionType; + expect(resultAPD.mappingRule?.length).equal(1); + }); + + it('autoPageDetection command parsing empty', () => { + const cmd: any = {}; + vars.autoPageDetection = processAutoPageDetectionCommand(cmd); + expect(isAutoPageDetectionEnabled()).equal(true); + expect(ignorePopstateEvent()).equal(false); + expect(titleAsPageNameInAutoPageDetection()).equal(false); + const resultAPD = vars.autoPageDetection as AutoPageDetectionType; + expect(typeof resultAPD).equal('object'); + expect(typeof resultAPD.mappingRule).equal('undefined'); + }); + + it('autoPageDetection command parsing abnormal configs (negative)', () => { + let cmd: any = 101; //number is unexpected + vars.autoPageDetection = processAutoPageDetectionCommand(cmd); + expect(isAutoPageDetectionEnabled()).equal(true); + let resultAPD = vars.autoPageDetection as AutoPageDetectionType; + expect(typeof resultAPD).equal('boolean'); + + cmd = 0; //number 0 is unexpected, 0 is treated as false + vars.autoPageDetection = processAutoPageDetectionCommand(cmd); + expect(isAutoPageDetectionEnabled()).equal(false); + + cmd = 'abc'; //string is unexpected + vars.autoPageDetection = processAutoPageDetectionCommand(cmd); + expect(isAutoPageDetectionEnabled()).equal(true); + resultAPD = vars.autoPageDetection as AutoPageDetectionType; + expect(typeof resultAPD).equal('boolean'); + + cmd = {titleAsPageName: 'abc'}; //string is unexpected for titleAsPageName + vars.autoPageDetection = processAutoPageDetectionCommand(cmd); + expect(isAutoPageDetectionEnabled()).equal(true); + expect(titleAsPageNameInAutoPageDetection()).equal(true); //string is treated as true + resultAPD = vars.autoPageDetection as AutoPageDetectionType; + expect(typeof resultAPD).equal('object'); + expect(typeof resultAPD.titleAsPageName).equal('string'); + }); + }); + describe('listen to various event listeners for page transition', () => { it('must listen to hashchange event and set page ', () => { @@ -167,6 +227,10 @@ describe('autodetection of page transition', () => { const title = ''; const url = 'http://localhost/#aboutNotGetMatched-about1Some-about2Where-about3SVL'; window.history.pushState(state, title, url); + + const titleResult: boolean = titleAsPageNameInAutoPageDetection(); + expect(titleResult).equal(false); + const beacon: Partial = {}; addCommonBeaconProperties(beacon); console.log('check the regex pattern ', beacon); @@ -181,12 +245,16 @@ describe('autodetection of page transition', () => { 'checkregex?matchId=' ] ], - titleAsPageName: undefined + titleAsPageName: false }; const state = {page: 10}; const title = ''; const url = 'http://localhost/checkregex12*benifits.-functions12replace-match.textpattern?matchId=6847664458'; window.history.pushState(state, title, url); + + const titleResult: boolean = titleAsPageNameInAutoPageDetection(); + expect(titleResult).equal(false); + const beacon: Partial = {}; addCommonBeaconProperties(beacon); expect(beacon.p).equal('checkregex?matchId=');