Skip to content

Commit e7e56f1

Browse files
Merge pull request #13459 from jerolimov/add-support-for-custom-segment-domains
OCPBUGS-25722: Add support for custom segment domains (to load JS and make API calls)
2 parents 4399622 + 2d065cb commit e7e56f1

2 files changed

Lines changed: 40 additions & 10 deletions

File tree

frontend/packages/console-telemetry-plugin/src/listeners/const.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
export const SEGMENT_API_KEY =
2-
window.SERVER_FLAGS?.telemetry?.DEVSANDBOX_SEGMENT_API_KEY ||
3-
window.SERVER_FLAGS?.telemetry?.SEGMENT_API_KEY ||
4-
'';
5-
61
export const TELEMETRY_DISABLED =
72
window.SERVER_FLAGS?.telemetry?.DISABLED === 'true' ||
83
window.SERVER_FLAGS?.telemetry?.DEVSANDBOX_DISABLED === 'true';

frontend/packages/console-telemetry-plugin/src/listeners/segment.ts

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,36 @@
11
import { TelemetryEventListener } from '@console/dynamic-plugin-sdk/src';
2-
import { SEGMENT_API_KEY, TELEMETRY_DISABLED, TELEMETRY_DEBUG } from './const';
2+
import { TELEMETRY_DISABLED, TELEMETRY_DEBUG } from './const';
3+
4+
/** Segmnet API Key that looks like a hash */
5+
const apiKey =
6+
window.SERVER_FLAGS?.telemetry?.DEVSANDBOX_SEGMENT_API_KEY ||
7+
window.SERVER_FLAGS?.telemetry?.SEGMENT_API_KEY ||
8+
'';
9+
10+
/**
11+
* Segment `apiHost` parameter that should have the format like `api.segment.io/v1`.
12+
* Is not defined here so that Segment can change it.
13+
*/
14+
const apiHost = window.SERVER_FLAGS?.telemetry?.SEGMENT_API_HOST || '';
15+
16+
/** Segment JS host. Default: `cdn.segment.com` */
17+
const jsHost = window.SERVER_FLAGS?.telemetry?.SEGMENT_JS_HOST || 'cdn.segment.com';
18+
19+
/** Full segment JS URL */
20+
const jsUrl =
21+
window.SERVER_FLAGS?.telemetry?.SEGMENT_JS_URL ||
22+
`https://${jsHost}/analytics.js/v1/${encodeURIComponent(apiKey)}/analytics.min.js`;
323

424
const initSegment = () => {
25+
if (TELEMETRY_DEBUG) {
26+
// eslint-disable-next-line no-console
27+
console.debug('console-telemetry-plugin: initialize segment API with:', {
28+
apiKey,
29+
apiHost,
30+
jsHost,
31+
jsUrl,
32+
});
33+
}
534
// eslint-disable-next-line no-multi-assign
635
const analytics = ((window as any).analytics = (window as any).analytics || []);
736
if (analytics.initialize) {
@@ -51,7 +80,7 @@ const initSegment = () => {
5180
const t = document.createElement('script');
5281
t.type = 'text/javascript';
5382
t.async = true;
54-
t.src = `https://cdn.segment.com/analytics.js/v1/${encodeURIComponent(key)}/analytics.min.js`;
83+
t.src = jsUrl;
5584
const n = document.getElementsByTagName('script')[0];
5685
if (n.parentNode) {
5786
n.parentNode.insertBefore(t, n);
@@ -60,10 +89,16 @@ const initSegment = () => {
6089
analytics._loadOptions = e;
6190
};
6291
analytics.SNIPPET_VERSION = '4.13.1';
63-
analytics.load(SEGMENT_API_KEY);
92+
const options: Record<string, any> = {};
93+
if (apiHost) {
94+
options.integrations = { 'Segment.io': { apiHost } };
95+
}
96+
analytics.load(apiKey, options);
6497
};
6598

66-
SEGMENT_API_KEY && !TELEMETRY_DISABLED && initSegment();
99+
if (!TELEMETRY_DISABLED && apiKey) {
100+
initSegment();
101+
}
67102

68103
const anonymousIP = {
69104
context: {
@@ -80,7 +115,7 @@ export const eventListener: TelemetryEventListener = async (
80115
console.debug('console-telemetry-plugin: received telemetry event:', eventType, properties);
81116
return;
82117
}
83-
if (!SEGMENT_API_KEY || TELEMETRY_DISABLED) {
118+
if (TELEMETRY_DISABLED || !apiKey) {
84119
return;
85120
}
86121
switch (eventType) {

0 commit comments

Comments
 (0)