Skip to content

Commit c613c89

Browse files
committed
refactor debug
1 parent 2fc4c9c commit c613c89

File tree

5 files changed

+89
-36
lines changed

5 files changed

+89
-36
lines changed

packages/signals/signals/src/core/debug-mode/index.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,21 @@
55
export const parseDebugModeQueryString = (): boolean | undefined => {
66
const queryParams = new URLSearchParams(window.location.search)
77

8-
const val = queryParams.get('segment_signals_debug')
8+
const val =
9+
queryParams.get('segment_signals_debug') ||
10+
queryParams.get('seg_signals_debug')
11+
if (val === 'true' || val === 'false') {
12+
return val === 'true'
13+
}
14+
return undefined
15+
}
16+
17+
export const parseDebugLoggingQueryString = (): boolean | undefined => {
18+
const queryParams = new URLSearchParams(window.location.search)
19+
20+
const val =
21+
queryParams.get('segment_signals_logging') ||
22+
queryParams.get('seg_signals_logging')
923
if (val === 'true' || val === 'false') {
1024
return val === 'true'
1125
}

packages/signals/signals/src/core/signals/settings.ts

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ export class SignalGlobalSettings {
3333
signalBuffer: SignalBufferSettingsConfig
3434
ingestClient: SignalsIngestSettingsConfig
3535
network: NetworkSettingsConfig
36+
signalsDebug: SignalsDebugSettings
3637

3738
private sampleSuccess = false
38-
private signalsDebug = new SignalsDebugSettings()
3939

4040
constructor(settings: SignalsSettingsConfig) {
4141
if (settings.maxBufferSize && settings.signalStorage) {
@@ -110,9 +110,11 @@ export class SignalGlobalSettings {
110110
}
111111
}
112112

113-
class SignalsDebugSettings {
113+
export class SignalsDebugSettings {
114+
private storageType = 'sessionStorage' as const
114115
private static redactionKey = 'segment_signals_debug_redaction_disabled'
115116
private static ingestionKey = 'segment_signals_debug_ingestion_enabled'
117+
116118
constructor(disableRedaction?: boolean, enableIngestion?: boolean) {
117119
if (typeof disableRedaction === 'boolean') {
118120
this.setDebugKey(SignalsDebugSettings.redactionKey, disableRedaction)
@@ -121,21 +123,22 @@ class SignalsDebugSettings {
121123
this.setDebugKey(SignalsDebugSettings.ingestionKey, enableIngestion)
122124
}
123125

124-
// setting ?segment_signals_debug=true will disable redaction, enable ingestion, and set keys in local storage
125-
// this setting will persist across page loads (even if there is no query string)
126-
// in order to clear the setting, user must set ?segment_signals_debug=false
127126
const debugModeInQs = parseDebugModeQueryString()
128127
logger.debug('debugMode is set to true via query string')
129128
if (typeof debugModeInQs === 'boolean') {
130-
this.setDebugKey(SignalsDebugSettings.redactionKey, debugModeInQs)
131-
this.setDebugKey(SignalsDebugSettings.ingestionKey, debugModeInQs)
129+
this.setAllDebugging(debugModeInQs)
132130
}
133131
}
134132

135-
setDebugKey(key: string, enable: boolean) {
133+
setAllDebugging(boolean: boolean) {
134+
this.setDebugKey(SignalsDebugSettings.redactionKey, boolean)
135+
this.setDebugKey(SignalsDebugSettings.ingestionKey, boolean)
136+
}
137+
138+
private setDebugKey(key: string, enable: boolean) {
136139
try {
137140
if (enable) {
138-
window.sessionStorage.setItem(key, 'true')
141+
window[this.storageType].setItem(key, 'true')
139142
} else {
140143
logger.debug(`Removing debug key ${key} from storage`)
141144
window.sessionStorage.removeItem(key)
@@ -145,13 +148,11 @@ class SignalsDebugSettings {
145148
}
146149
}
147150

148-
getDisableSignalsRedaction() {
151+
private getDebugKey(key: string): boolean {
149152
try {
150-
const isEnabled = Boolean(
151-
window.sessionStorage.getItem(SignalsDebugSettings.redactionKey)
152-
)
153+
const isEnabled = Boolean(window[this.storageType].getItem(key))
153154
if (isEnabled) {
154-
logger.debug(`${SignalsDebugSettings.redactionKey}=true (app. storage)`)
155+
logger.debug(`${key}=true (app. storage)`)
155156
return true
156157
}
157158
} catch (e) {
@@ -160,18 +161,11 @@ class SignalsDebugSettings {
160161
return false
161162
}
162163

163-
getEnableSignalsIngestion() {
164-
try {
165-
const isEnabled = Boolean(
166-
window.sessionStorage.getItem(SignalsDebugSettings.ingestionKey)
167-
)
168-
if (isEnabled) {
169-
logger.debug(`${SignalsDebugSettings.ingestionKey}=true (app. storage)`)
170-
return true
171-
}
172-
} catch (e) {
173-
logger.debug('Storage error', e)
174-
}
175-
return false
164+
getDisableSignalsRedaction(): boolean {
165+
return this.getDebugKey(SignalsDebugSettings.redactionKey)
166+
}
167+
168+
getEnableSignalsIngestion(): boolean {
169+
return this.getDebugKey(SignalsDebugSettings.ingestionKey)
176170
}
177171
}

packages/signals/signals/src/core/signals/signals.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ export class Signals implements ISignals {
129129
void this.buffer.clear()
130130
}
131131

132+
debug() {
133+
this.globalSettings.signalsDebug.setDebugMode(true)
134+
logger.enableDebugLogging()
135+
}
136+
132137
/**
133138
* Emit custom signals.
134139
*/

packages/signals/signals/src/lib/logger/index.ts

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,47 @@
1+
import { parseDebugLoggingQueryString } from '../../core/debug-mode'
2+
13
class Logger {
2-
globalKey = 'SEGMENT_SIGNALS_DEBUG'
3-
get debugLoggingEnabled() {
4-
return (window as any)[this.globalKey] === true
4+
private static loggingKey = 'segment_signals_debug_logging_enabled'
5+
constructor() {
6+
const val = parseDebugLoggingQueryString()
7+
if (typeof val === 'boolean') {
8+
this.setDebugKey(Logger.loggingKey, val)
9+
}
10+
}
11+
12+
private debugLoggingEnabled(): boolean {
13+
try {
14+
const isEnabled = Boolean(
15+
window.sessionStorage.getItem(Logger.loggingKey)
16+
)
17+
if (isEnabled) {
18+
return true
19+
}
20+
} catch (e) {
21+
logger.debug('Storage error', e)
22+
}
23+
return false
24+
}
25+
26+
private setDebugKey(key: string, enable: boolean) {
27+
try {
28+
if (enable) {
29+
window.sessionStorage.setItem(key, 'true')
30+
} else {
31+
logger.debug(`Removing debug key ${key} from storage`)
32+
window.sessionStorage.removeItem(key)
33+
}
34+
} catch (e) {
35+
logger.debug('Storage error', e)
36+
}
537
}
638

7-
enableDebugLogging() {
8-
;(window as any)[this.globalKey] = true
39+
enableDebugLogging(bool = true) {
40+
this.setDebugKey(Logger.loggingKey, bool)
941
}
1042

1143
debug(...args: any[]): void {
12-
if (this.debugLoggingEnabled) {
44+
if (this.debugLoggingEnabled()) {
1345
console.log('[signals debug]', ...args)
1446
}
1547
}

packages/signals/signals/src/plugin/signals-plugin.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Plugin } from '@segment/analytics-next'
2-
import { Signals } from '../core/signals'
2+
import { Signals, SignalsDebugSettings } from '../core/signals'
33
import { logger } from '../lib/logger'
44
import { AnyAnalytics, SignalsPluginSettingsConfig } from '../types'
55
import { Signal } from '@segment/analytics-signals-runtime'
@@ -26,12 +26,16 @@ export class SignalsPlugin implements Plugin, SignalsAugmentedFunctionality {
2626
readonly name = 'SignalsPlugin'
2727
readonly version = version
2828
public signals: Signals
29-
3029
constructor(settings: SignalsPluginSettingsConfig = {}) {
3130
assertBrowserEnv()
31+
32+
// assign to window for debugging purposes
33+
Object.assign(window, { SegmentSignalsPlugin: this })
34+
3235
if (settings.enableDebugLogging) {
3336
logger.enableDebugLogging()
3437
}
38+
3539
logger.debug('SignalsPlugin initializing', { settings })
3640

3741
this.signals = new Signals({
@@ -79,4 +83,8 @@ export class SignalsPlugin implements Plugin, SignalsAugmentedFunctionality {
7983
this.signals.signalEmitter.emit(signal)
8084
return this
8185
}
86+
87+
debug(enable = true) {
88+
this.signals.debug(enable)
89+
}
8290
}

0 commit comments

Comments
 (0)