Skip to content

Commit f06ab40

Browse files
Add signup logic to handle adblock (#28851)
* refactor logic * remove src attr by default * readd * refactor * change path * intentionally remove fingerprint * cleanup
1 parent 3bdbb5d commit f06ab40

File tree

5 files changed

+215
-127
lines changed

5 files changed

+215
-127
lines changed
+1-101
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,6 @@
11
import { DOMReady } from '../helpers/documentReady';
2-
import { isMobile } from '../utils/isMobile';
3-
import { getGeoloc, getAppBaseUrl } from 'geo-locate';
4-
import { UTMCookies } from '../utms';
52

63
const doOnLoad = () => {
7-
const signupModal = document.getElementById('signupModal');
8-
9-
const getLanguageParam = () => {
10-
let langParam = '';
11-
let lang = '';
12-
13-
if (document.documentElement.lang) {
14-
lang = document.documentElement.lang;
15-
} else {
16-
lang = ddc.lang;
17-
}
18-
19-
if (lang === 'fr' || lang === 'ja' || lang === 'ko' || lang === 'es') {
20-
langParam = `lang=${lang}`;
21-
} else {
22-
langParam = '';
23-
}
24-
25-
return langParam;
26-
};
27-
28-
const appendUrlQueryParams = (url) => {
29-
let completeUrl = url;
30-
let operator = completeUrl.includes('?') ? '&' : '?';
31-
const currentUrl = new URL(window.location.href);
32-
const currentParams = currentUrl.searchParams;
33-
34-
// If non-english lang, append param
35-
if (getLanguageParam()) {
36-
completeUrl += `${operator}${getLanguageParam()}`;
37-
operator = '&';
38-
}
39-
40-
// If mobile, append param
41-
if (isMobile()) {
42-
completeUrl += `${operator}mobile=true`;
43-
operator = '&';
44-
}
45-
46-
// Convert selected cookies to query params so they are preserved during signup process per https://datadoghq.atlassian.net/browse/WEB-4695
47-
const allCookies = UTMCookies.getAll();
48-
let customSignupUTM = ['gclid', 'MSCLKID', '_mkto_trk'];
49-
for (const [key, value] of Object.entries(allCookies)) {
50-
if (customSignupUTM.includes(key)) {
51-
completeUrl += `${operator}${key}=${encodeURIComponent(value)}`;
52-
operator = '&';
53-
}
54-
}
55-
56-
// Rebuild UTM params from original query params
57-
let utmParam = false;
58-
currentParams.forEach((value, key) => {
59-
if (key.startsWith('utm')) {
60-
utmParam = true;
61-
completeUrl += `${operator}${`dd-${key.replace('_', '-')}`}=${encodeURIComponent(value)}`;
62-
operator = '&';
63-
}
64-
});
65-
66-
// If no UTM values were detected in query params, check to see if they already exist as a cookie
67-
if (!utmParam) {
68-
const allCookies = UTMCookies.getAll();
69-
for (const [key, value] of Object.entries(allCookies)) {
70-
if (key.includes('dd-utm')) {
71-
completeUrl += `${operator}${key}=${encodeURIComponent(value)}`;
72-
operator = '&';
73-
}
74-
}
75-
}
76-
77-
// Try to append RUM device ID and session ID
78-
if(window.DD_RUM) {
79-
const rumDeviceId = window.DD_RUM.getUser() ? window.DD_RUM.getUser().device_id : null;
80-
if(rumDeviceId) {
81-
completeUrl += `${operator}dd-device-id=${rumDeviceId}`;
82-
operator = '&';
83-
}
84-
const rumSessionId = window.DD_RUM.getInternalContext() ? window.DD_RUM.getInternalContext().session_id : null;
85-
if(rumSessionId) {
86-
completeUrl += `${operator}dd-session-id=${rumSessionId}`;
87-
operator = '&';
88-
}
89-
}
90-
91-
return completeUrl;
92-
};
93-
94-
signupModal.addEventListener('show.bs.modal', () => {
95-
96-
getGeoloc().then((loc) => {
97-
const baseUrl = `https://${getAppBaseUrl(loc.appRegion)}/signup_corp`;
98-
document.querySelector('#signUpIframe').setAttribute('src', appendUrlQueryParams(baseUrl));;
99-
});
100-
});
101-
102-
signupModal.addEventListener('hide.bs.modal', () => {
103-
document.querySelector('#signUpIframe').setAttribute('src', '');
104-
});
1054

1065
const imageModal = document.getElementById('popupImageModal');
1076
let modalContent, modalBody, modalDialog;
@@ -201,6 +100,7 @@ const doOnLoad = () => {
201100
}
202101
}
203102
}
103+
204104
};
205105

206106
DOMReady(doOnLoad);

assets/scripts/components/signup.js

+205
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
import { DOMReady } from '../helpers/documentReady';
2+
import { isMobile } from '../utils/isMobile';
3+
import { getGeoloc, getAppBaseUrl } from 'geo-locate';
4+
import { UTMCookies } from '../utms';
5+
import { getSignupFailover } from 'signup-failover';
6+
import Modal from 'bootstrap/js/dist/modal';
7+
8+
const signupIframe = document.querySelector('#signUpIframe');
9+
const ddc = {
10+
lang: "",
11+
env: document.documentElement.dataset.env,
12+
getLanguage: function () {
13+
const langAttr = document.querySelector("html").lang;
14+
},
15+
};
16+
17+
const getLanguageParam = () => {
18+
let lang = "";
19+
let langParam = "";
20+
21+
if (document.documentElement.lang) {
22+
lang = document.documentElement.lang;
23+
} else {
24+
lang = ddc.lang;
25+
}
26+
27+
// TODO this is likely broken if ddc.lang returns full name. Needs to be fixed
28+
if (
29+
lang === "fr" ||
30+
lang === "es" ||
31+
lang === "de" ||
32+
lang === "ja" ||
33+
lang === "ko"
34+
) {
35+
langParam = `lang=${lang}`;
36+
} else {
37+
langParam = "";
38+
}
39+
40+
return langParam;
41+
};
42+
43+
const appendUrlQueryParams = (url) => {
44+
let completeUrl = url;
45+
let operator = completeUrl.includes("?") ? "&" : "?";
46+
const currentUrl = new URL(window.location.href);
47+
const currentParams = currentUrl.searchParams;
48+
49+
// If non-english lang, append param
50+
if (getLanguageParam()) {
51+
completeUrl += `${operator}${getLanguageParam()}`;
52+
operator = "&";
53+
}
54+
55+
// If mobile, append param
56+
if (isMobile()) {
57+
completeUrl += `${operator}mobile=true`;
58+
operator = "&";
59+
}
60+
61+
const allCookies = UTMCookies.getAll();
62+
let customSignupUTM = ["gclid", "MSCLKID", "_mkto_trk"]; // harcoded list of UTM params to preserve
63+
for (const [key, value] of Object.entries(allCookies)) {
64+
// If the cookie value exists in the pre-defined list of desired UTM params, append it to the URL
65+
if (customSignupUTM.includes(key)) {
66+
completeUrl += `${operator}${key}=${encodeURIComponent(value)}`;
67+
operator = "&";
68+
}
69+
}
70+
71+
// Rebuild UTM params from original query params
72+
let utmParam = false;
73+
currentParams.forEach((value, key) => {
74+
if (key.startsWith("utm")) {
75+
utmParam = true;
76+
completeUrl += `${operator}${`dd-${key.replace(
77+
"_",
78+
"-"
79+
)}`}=${encodeURIComponent(value)}`;
80+
operator = "&";
81+
}
82+
});
83+
84+
// If no UTM values were detected in query params, check to see if they already exist as a cookie
85+
if (!utmParam) {
86+
const allCookies = UTMCookies.getAll();
87+
for (const [key, value] of Object.entries(allCookies)) {
88+
if (key.includes("dd-utm")) {
89+
completeUrl += `${operator}${key}=${encodeURIComponent(value)}`;
90+
operator = "&";
91+
}
92+
}
93+
}
94+
95+
// Try to append RUM device ID and session ID
96+
if (window.DD_RUM) {
97+
const rumDeviceId = window.DD_RUM.getUser()
98+
? window.DD_RUM.getUser().device_id
99+
: null;
100+
if (rumDeviceId) {
101+
completeUrl += `${operator}dd-device-id=${rumDeviceId}`;
102+
operator = "&";
103+
}
104+
const rumSessionId = window.DD_RUM.getInternalContext()
105+
? window.DD_RUM.getInternalContext().session_id
106+
: null;
107+
if (rumSessionId) {
108+
completeUrl += `${operator}dd-session-id=${rumSessionId}`;
109+
operator = "&";
110+
}
111+
}
112+
113+
// Attempt to extract sweepstakes URL
114+
const sweepstakesUrl = document.getElementById("signup")
115+
? document.getElementById("signup").getAttribute("data-sweepstakes-url")
116+
: "";
117+
if (sweepstakesUrl) {
118+
completeUrl += `${operator}sweepstakes_url=${encodeURIComponent(
119+
sweepstakesUrl
120+
)}`;
121+
operator = "&";
122+
}
123+
124+
return completeUrl;
125+
};
126+
127+
const loadSignupFormInFrame = (iframeElement, url) => {
128+
if (iframeElement) {
129+
iframeElement.src = url;
130+
131+
// handle a loading issue
132+
let iframeLoadIssueHandler = () => {
133+
let newUrl = url.replace("_corp", "");
134+
window.open(newUrl);
135+
136+
const modalBody = document.querySelector(
137+
"#signupModal > div > .modal-content"
138+
);
139+
modalBody.innerHTML = `<div class="text-center small w-100 my-3 p-5">Signup Form loading error. <a href="${newUrl}" class="text-underline text-purple-600" target=_blank>Click here</a> to open in a new window</div>`;
140+
};
141+
142+
// if adblock detected & we are trying to load eu signup iframe then show error and open in a new tab
143+
// until datadoghq.eu is removed from 3rd party blocking lists
144+
if (url.indexOf(".eu") !== -1 && !document.getElementById("TxFnCdowUQWB")) {
145+
iframeLoadIssueHandler();
146+
}
147+
148+
openSignupModal();
149+
}
150+
};
151+
152+
document.querySelectorAll('.sign-up-trigger').forEach(item => {
153+
item.addEventListener('click', (event) => {
154+
event.preventDefault();
155+
156+
getSignupFailover().then((failoverEnabled) => {
157+
if (failoverEnabled) {
158+
const demoModal = document.querySelector('#signupDemo');
159+
const signupDemoModal = demoModal ? new Modal(demoModal) : null;
160+
if(signupDemoModal) {
161+
signupDemoModal.show(item);
162+
}
163+
} else {
164+
initSignup();
165+
}
166+
});
167+
})
168+
})
169+
170+
const initSignup = () => {
171+
let baseUrl = "";
172+
let completeUrl = "";
173+
let cleanCenter = "true";
174+
let eventV2Param = "&event_v2=true";
175+
176+
if (document.querySelector(".clean-center-false")) {
177+
cleanCenter = "false";
178+
}
179+
180+
// Event v2 cannot simply be set to false, it must be omitted
181+
if (document.querySelector(".event-v2-false")) {
182+
eventV2Param = "";
183+
}
184+
185+
// Get users location and set the signup url accordingly
186+
getGeoloc().then((loc) => {
187+
baseUrl = `https://${getAppBaseUrl(loc.appRegion)}/signup_corp?lang=${
188+
document.documentElement.lang
189+
}`;
190+
completeUrl = appendUrlQueryParams(baseUrl);
191+
loadSignupFormInFrame(signupIframe, completeUrl);
192+
});
193+
};
194+
195+
const openSignupModal = () => {
196+
const signupModal = new Modal(document.getElementById('signupModal'));
197+
signupModal.show();
198+
};
199+
200+
const handleDOMReady = () => {
201+
// Get users location during page load so dont have to wait on CTA click
202+
getGeoloc();
203+
};
204+
205+
DOMReady(handleDOMReady);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const e = document.createElement('div');
2+
e.id = 'TxFnCdowUQWB';
3+
e.style.display = 'none';
4+
document.body.appendChild(e);

assets/scripts/main-dd-js.js

+1-25
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import Modal from 'bootstrap/js/dist/modal';
21
import Tooltip from 'bootstrap/js/dist/tooltip';
3-
import { getSignupFailover } from 'signup-failover';
42

53
import 'bootstrap/js/dist/dropdown';
64
import 'bootstrap/js/dist/tab';
@@ -20,29 +18,7 @@ import './components/bootstrap-dropdown-custom';
2018
import './components/navbar'; // should move this to websites-modules
2119
import './components/mobile-nav'; // should move this to websites-modules
2220
import './components/accordion-auto-open';
23-
24-
// Event handlers
25-
document.querySelectorAll('.sign-up-trigger').forEach(item => {
26-
item.addEventListener('click', (event) => {
27-
event.preventDefault();
28-
29-
getSignupFailover().then((failoverEnabled) => {
30-
if (failoverEnabled) {
31-
const demoModal = document.querySelector('#signupDemo');
32-
const signupDemoModal = demoModal ? new Modal(demoModal) : null;
33-
if(signupDemoModal) {
34-
signupDemoModal.show(item);
35-
}
36-
} else {
37-
const signupModal = new Modal(document.getElementById('signupModal'))
38-
signupModal.show(item)
39-
}
40-
});
41-
})
42-
})
43-
44-
// TODO: split up code from datadog-docs.js into modules after webpack migration
45-
// import './components/sidenav';
21+
import './components/signup';
4622

4723
// Add Bootstrap Tooltip across docs
4824
const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]')

layouts/partials/footer-scripts.html

+4-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@
5858

5959
{{- $tooltipOpts := dict "targetPath" "static/tooltip.js" "defines" $defines "sourceMap" $sourcemap "minify" $isProd -}}
6060
{{- $tooltipJs := resources.Get "scripts/components/tooltip.js" | js.Build $tooltipOpts -}}
61+
{{- $anePopupBannerOpts := dict "targetPath" "static/ane-popup-banner.js" "defines" $defines "sourceMap" $sourcemap "minify" $isProd -}}
62+
{{- $anePopupBannerJs := resources.Get "scripts/helpers/ane-popup-banner.js" | js.Build $anePopupBannerOpts -}}
6163
{{- if $isProd -}}
6264
{{- $tooltipJs = $tooltipJs | fingerprint "sha512" -}}
6365
{{- end -}}
64-
<script type="text/javascript" src="{{ $tooltipJs.Permalink }}" {{ if $isProd }} integrity="{{ $tooltipJs.Data.Integrity }}" {{ end }}></script>
66+
<script type="text/javascript" src="{{ $tooltipJs.Permalink }}" {{ if $isProd }} integrity="{{ $tooltipJs.Data.Integrity }}" {{ end }}></script>
67+
<script type="text/javascript" src="{{ $anePopupBannerJs.Permalink }}" {{ if $isProd }} integrity="{{ $anePopupBannerJs.Data.Integrity }}" {{ end }}></script>

0 commit comments

Comments
 (0)