Skip to content

Commit 6ae4940

Browse files
committed
Telemetry update
1 parent 245ebc0 commit 6ae4940

File tree

3 files changed

+99
-3
lines changed

3 files changed

+99
-3
lines changed

Diff for: .github/workflows/build.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
- name: "Build the site"
1818
run: |
1919
npm install
20-
npm run build
20+
NEXT_ENV_DESCRIPTOR=production NEXT_ONE_DS_INSTRUMENTATION_KEY=b341ec446e65436485df678003ce82f7 npm run build
2121
- name: "Stamp content.json"
2222
env:
2323
GITSHA: ${{ github.sha }}

Diff for: src/components/TelemetryScript.tsx

+33-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,38 @@
66
import fs from 'fs';
77

88
const telemetryJavaScript = fs.readFileSync('./src/components/telemetry.js', 'utf8');
9+
const REPLACEMENT_KEY_CONST = "const oneDsTelemetryInstrumentationKey = '';";
10+
const REPLACEMENT_ENV_CONST = "const oneDsEnvironmentDescriptor = '';";
11+
const REPLACEMENT_IS_DEBUGGING_CONST = "const isDebugging = false;";
12+
13+
function get1dsInstrumentationKey() {
14+
return process.env.NEXT_ONE_DS_INSTRUMENTATION_KEY;
15+
}
16+
17+
function getIsDebugging() {
18+
return process.env.NEXT_DEBUG === '1';
19+
}
20+
21+
function getTelemetryEnvironmentDescriptor() {
22+
return process.env.NEXT_ENV_DESCRIPTOR || 'unknown';
23+
}
24+
25+
function getTelemetryScript() {
26+
let script = telemetryJavaScript;
27+
const instrumentationKey = get1dsInstrumentationKey();
28+
const env = getTelemetryEnvironmentDescriptor();
29+
const isDebugging = getIsDebugging();
30+
if (isDebugging) {
31+
script = script.replace(REPLACEMENT_IS_DEBUGGING_CONST, `const isDebugging = true;`);
32+
}
33+
if (instrumentationKey) {
34+
script = script.replace(REPLACEMENT_KEY_CONST, `const oneDsTelemetryInstrumentationKey = '${instrumentationKey}';`);
35+
}
36+
if (env) {
37+
script = script.replace(REPLACEMENT_ENV_CONST, `const oneDsEnvironmentDescriptor = '${env}';`);
38+
}
39+
return script;
40+
}
941

1042
export default function TelemetryScript() {
1143
return (
@@ -14,7 +46,7 @@ export default function TelemetryScript() {
1446
<script
1547
id="uhf-footer-ccpa"
1648
dangerouslySetInnerHTML={{
17-
__html: telemetryJavaScript,
49+
__html: getTelemetryScript(),
1850
}}
1951
/>
2052
</>

Diff for: src/components/telemetry.js

+65-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,65 @@
11
// Detect GPC
22
const globalPrivacyControlEnabled = navigator.globalPrivacyControl;
33

4+
const isDebugging = false;
5+
const oneDsTelemetryInstrumentationKey = '';
6+
const oneDsEnvironmentDescriptor = '';
7+
8+
// 1DS telemetry
9+
// ---
10+
function inject1ds() {
11+
if (!oneDsTelemetryInstrumentationKey) {
12+
return;
13+
}
14+
const script = document.createElement('script');
15+
script.type = 'text/javascript';
16+
script.src = 'https://js.monitor.azure.com/scripts/c/ms.analytics-web-3.min.js';
17+
script.async = true;
18+
script.onload = () => {
19+
initialize1dsAnalytics(oneDsTelemetryInstrumentationKey);
20+
};
21+
document.head.appendChild(script);
22+
}
23+
24+
function initialize1dsAnalytics(instrumentationKey) {
25+
// Make sure the oneDS object is available
26+
if (typeof window['oneDS'] === 'undefined') {
27+
return;
28+
}
29+
const oneDS = window['oneDS'];
30+
const ApplicationInsights = oneDS.ApplicationInsights;
31+
if (!ApplicationInsights) {
32+
return;
33+
}
34+
35+
const analytics = new ApplicationInsights();
36+
const env = oneDsEnvironmentDescriptor || 'unknown';
37+
const config = {
38+
instrumentationKey: instrumentationKey,
39+
channelConfiguration: {
40+
eventsLimitInMem: 50
41+
},
42+
propertyConfiguration: {
43+
env,
44+
},
45+
webAnalyticsConfiguration: {
46+
autoCapture: {
47+
scroll: true,
48+
pageView: true,
49+
onLoad: true,
50+
onUnload: true,
51+
click: true,
52+
resize: true,
53+
jsError: true
54+
}
55+
}
56+
};
57+
analytics.initialize(config, []);
58+
}
59+
60+
// Cookie consent management
61+
// ---
62+
463
// Set data sharing opt-in to false when GPC/AMC controls detected
564
const GPC_DataSharingOptIn = (globalPrivacyControlEnabled) ? false : checkThirdPartyAdsOptOutCookie();
665

@@ -9,7 +68,7 @@ function checkThirdPartyAdsOptOutCookie() {
968
try {
1069
const ThirdPartyAdsOptOutCookieName = '3PAdsOptOut';
1170
var cookieValue = getCookie(ThirdPartyAdsOptOutCookieName);
12-
71+
1372
//for unauthenticated users
1473
return cookieValue != 1;
1574
} catch {
@@ -26,6 +85,10 @@ function enableAppInsights() {
2685
var appInsights=window.appInsights||function(config){function s(config){t[config]=function(){var i=arguments;t.queue.push(function(){t[config].apply(t,i)})}}var t={config:config},r=document,f=window,e="script",o=r.createElement(e),i,u;for(o.src=config.url||"//az416426.vo.msecnd.net/scripts/a/ai.0.js",r.getElementsByTagName(e)[0].parentNode.appendChild(o),t.cookie=r.cookie,t.queue=[],i=["Event","Exception","Metric","PageView","Trace"];i.length;)s("track"+i.pop());return config.disableExceptionTracking||(i="onerror",s("_"+i),u=f[i],f[i]=function(config,r,f,e,o){var s=u&&u(config,r,f,e,o);return s!==!0&&t["_"+i](config,r,f,e,o),s}),t}({instrumentationKey:"98f90d2e-54e0-4e9c-a919-deea70bad056"/*the telemetry instrumentation key is NOT a secret FYI */});window.appInsights=appInsights;appInsights.trackPageView();
2786
}
2887

88+
function enable1ds() {
89+
inject1ds();
90+
}
91+
2992
var siteConsent = null;
3093

3194
const currentUrl = window.location.href;
@@ -46,6 +109,7 @@ function enableAnalytics() {
46109
function addTelemetry() {
47110
enableAppInsights();
48111
enableAnalytics();
112+
enable1ds();
49113
}
50114
function onConsentChanged(categoryPreferences) {
51115
showTelemetryDebug && console.log('onConsentChanged.');

0 commit comments

Comments
 (0)