Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release-4.14] OCPBUGS-27898: Add support for custom segment domains (to load JS and make API calls) #13540

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
export const SEGMENT_API_KEY =
window.SERVER_FLAGS?.telemetry?.DEVSANDBOX_SEGMENT_API_KEY ||
window.SERVER_FLAGS?.telemetry?.SEGMENT_API_KEY ||
'';

export const TELEMETRY_DISABLED =
window.SERVER_FLAGS?.telemetry?.DISABLED === 'true' ||
window.SERVER_FLAGS?.telemetry?.DEVSANDBOX_DISABLED === 'true';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,36 @@
import { TelemetryEventListener } from '@console/dynamic-plugin-sdk/src';
import { SEGMENT_API_KEY, TELEMETRY_DISABLED, TELEMETRY_DEBUG } from './const';
import { TELEMETRY_DISABLED, TELEMETRY_DEBUG } from './const';

/** Segmnet API Key that looks like a hash */
const apiKey =
window.SERVER_FLAGS?.telemetry?.DEVSANDBOX_SEGMENT_API_KEY ||
window.SERVER_FLAGS?.telemetry?.SEGMENT_API_KEY ||
'';

/**
* Segment `apiHost` parameter that should have the format like `api.segment.io/v1`.
* Is not defined here so that Segment can change it.
*/
const apiHost = window.SERVER_FLAGS?.telemetry?.SEGMENT_API_HOST || '';

/** Segment JS host. Default: `cdn.segment.com` */
const jsHost = window.SERVER_FLAGS?.telemetry?.SEGMENT_JS_HOST || 'cdn.segment.com';

/** Full segment JS URL */
const jsUrl =
window.SERVER_FLAGS?.telemetry?.SEGMENT_JS_URL ||
`https://${jsHost}/analytics.js/v1/${encodeURIComponent(apiKey)}/analytics.min.js`;

const initSegment = () => {
if (TELEMETRY_DEBUG) {
// eslint-disable-next-line no-console
console.debug('console-telemetry-plugin: initialize segment API with:', {
apiKey,
apiHost,
jsHost,
jsUrl,
});
}
// eslint-disable-next-line no-multi-assign
const analytics = ((window as any).analytics = (window as any).analytics || []);
if (analytics.initialize) {
Expand Down Expand Up @@ -51,7 +80,7 @@ const initSegment = () => {
const t = document.createElement('script');
t.type = 'text/javascript';
t.async = true;
t.src = `https://cdn.segment.com/analytics.js/v1/${encodeURIComponent(key)}/analytics.min.js`;
t.src = jsUrl;
const n = document.getElementsByTagName('script')[0];
if (n.parentNode) {
n.parentNode.insertBefore(t, n);
Expand All @@ -60,10 +89,16 @@ const initSegment = () => {
analytics._loadOptions = e;
};
analytics.SNIPPET_VERSION = '4.13.1';
analytics.load(SEGMENT_API_KEY);
const options: Record<string, any> = {};
if (apiHost) {
options.integrations = { 'Segment.io': { apiHost } };
}
analytics.load(apiKey, options);
};

SEGMENT_API_KEY && !TELEMETRY_DISABLED && initSegment();
if (!TELEMETRY_DISABLED && apiKey) {
initSegment();
}

const anonymousIP = {
context: {
Expand All @@ -80,7 +115,7 @@ export const eventListener: TelemetryEventListener = async (
console.debug('console-telemetry-plugin: received telemetry event:', eventType, properties);
return;
}
if (!SEGMENT_API_KEY || TELEMETRY_DISABLED) {
if (TELEMETRY_DISABLED || !apiKey) {
return;
}
switch (eventType) {
Expand Down