Skip to content

Commit a154765

Browse files
Merge pull request #60 from IABTechLab/eee-UID2-6129-secure-signals-client-server-integrations-fix
Updae secure signals client server integrations fix
2 parents 7a79f2f + c87045d commit a154765

File tree

4 files changed

+113
-78
lines changed

4 files changed

+113
-78
lines changed

web-integrations/google-secure-signals/client-server/server.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,13 @@ app.set('view engine', 'html');
3737

3838
app.get('/', (req, res) => {
3939
res.render('index', {
40+
identity: undefined,
41+
isOptout: false,
4042
uidBaseUrl,
4143
uidJsSdkUrl,
4244
uidJsSdkName,
4345
secureSignalsSdkUrl,
46+
secureSignalsStorageKey: process.env.UID_SECURE_SIGNALS_STORAGE_KEY,
4447
identityName,
4548
docsBaseUrl
4649
});
@@ -132,11 +135,14 @@ app.post('/login', async (req, res) => {
132135
const response = decrypt(encryptedResponse.data, uidClientSecret, nonce);
133136

134137
if (response.status === 'optout') {
135-
res.render('optout', {
138+
res.render('index', {
139+
identity: null,
140+
isOptout: true,
136141
uidBaseUrl,
137142
uidJsSdkUrl,
138143
uidJsSdkName,
139144
secureSignalsSdkUrl,
145+
secureSignalsStorageKey: process.env.UID_SECURE_SIGNALS_STORAGE_KEY,
140146
identityName,
141147
docsBaseUrl
142148
});
@@ -155,12 +161,14 @@ app.post('/login', async (req, res) => {
155161
docsBaseUrl
156162
});
157163
} else {
158-
res.render('login', {
164+
res.render('index', {
159165
identity: response.body,
166+
isOptout: false,
160167
uidBaseUrl,
161168
uidJsSdkUrl,
162169
uidJsSdkName,
163170
secureSignalsSdkUrl,
171+
secureSignalsStorageKey: process.env.UID_SECURE_SIGNALS_STORAGE_KEY,
164172
identityName,
165173
docsBaseUrl
166174
});

web-integrations/google-secure-signals/client-server/views/index.html

Lines changed: 103 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
<link rel="stylesheet" type="text/css" href="/stylesheets/style.css" />
88
<link rel="shortcut icon" href="/images/favicon.png" />
99
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
10+
<script src="<%- uidJsSdkUrl %>"></script>
11+
<script src="<%- secureSignalsSdkUrl %>"></script>
12+
<script async src="https://securepubads.g.doubleclick.net/tag/js/gpt.js"></script>
1013

1114
<script>
1215
$(document).ready(() => {
@@ -23,16 +26,63 @@
2326
$("#login_required").html(sdk.isLoginRequired() ? "yes" : "no");
2427
$("#update_counter").html(callbackCounter);
2528
$("#identity_state").html(String(JSON.stringify(payload, null, 2)));
29+
30+
// Update Secure Signals values displayed in the GUI
31+
updateSecureSignals();
2632

33+
<% if (isOptout) { %>
34+
$("#login_form").hide();
35+
$("#logout_form").hide();
36+
$("#optout_form").show();
37+
$("#optout_banner").show();
38+
$('#googleAdContainer').hide();
39+
<% } else { %>
40+
// before token generation, show login form
2741
if (sdk.isLoginRequired()) {
2842
$("#login_form").show();
2943
$("#logout_form").hide();
44+
$("#optout_form").hide();
45+
$("#optout_banner").hide();
3046
$('#googleAdContainer').hide();
3147
} else {
48+
// Success case - token generated, show logout form
3249
$("#login_form").hide();
3350
$("#logout_form").show();
51+
$("#optout_form").hide();
52+
$("#optout_banner").hide();
3453
$('#googleAdContainer').show();
3554
}
55+
<% } %>
56+
}
57+
58+
function updateSecureSignals() {
59+
try {
60+
// Read from localStorage
61+
const secureSignalsStorageKey = '<%- secureSignalsStorageKey %>';
62+
const secureSignalsStorage = localStorage[secureSignalsStorageKey];
63+
const token = sdk.getAdvertisingToken();
64+
65+
// Safety net: If token exists but Secure Signals haven't loaded yet, reload the page
66+
if (token && !secureSignalsStorage && !<%= isOptout %>) {
67+
console.log("Token exists but Secure Signals not loaded yet, reloading page...");
68+
location.reload();
69+
return;
70+
}
71+
72+
const secureSignalsStorageJson = secureSignalsStorage && JSON.parse(secureSignalsStorage);
73+
74+
if (secureSignalsStorageJson && secureSignalsStorageJson[1]) {
75+
$("#secure_signals_loaded").html("yes");
76+
$("#secure_signals_value").html(JSON.stringify(secureSignalsStorageJson, null, 2));
77+
} else {
78+
$("#secure_signals_loaded").html("no");
79+
$("#secure_signals_value").html("undefined");
80+
}
81+
} catch (e) {
82+
console.log("Secure signals not yet available", e);
83+
$("#secure_signals_loaded").html("no");
84+
$("#secure_signals_value").html("undefined");
85+
}
3686
}
3787

3888
function onIdentityUpdated(eventType, payload) {
@@ -42,30 +92,62 @@
4292
) {
4393
++callbackCounter;
4494
}
45-
updateGuiElements(payload);
95+
// Allow secure signals time to load
96+
setTimeout(() => updateGuiElements(payload), 1000);
4697
}
4798

4899
$("#logout").click(() => {
49-
sdk.disconnect();
50100
window.googletag.secureSignalProviders.clearAllCache();
51-
updateGuiElements(undefined);
101+
sdk.disconnect();
102+
window.location.href = '/';
52103
});
53-
$("#login").click(() => {
104+
105+
$("#try_another").click(() => {
54106
window.googletag.secureSignalProviders.clearAllCache();
107+
sdk.disconnect();
108+
window.location.href = '/';
55109
});
110+
56111
sdk.callbacks.push((eventType, payload) => {
57112
if (eventType === "SdkLoaded") {
58113
sdk.init({
59114
baseUrl: "<%- uidBaseUrl %>",
60-
enableSecureSignals: true,
61115
});
62116
}
63117
});
118+
119+
sdk.callbacks.push((eventType, payload) => {
120+
if (eventType === 'InitCompleted') {
121+
<% if (identity !== null && typeof identity !== 'undefined') { %>
122+
// Server provided an identity, set it
123+
if (sdk.isLoginRequired()) {
124+
sdk.setIdentity(<%- JSON.stringify(identity) %>);
125+
} else {
126+
// Identity already exists, just update GUI
127+
updateGuiElements(payload);
128+
}
129+
<% } else { %>
130+
// No identity from server (including opt-out case)
131+
// SDK will naturally remain in "login required" state
132+
updateGuiElements(payload);
133+
<% if (isOptout) { %>
134+
$("#optout_banner").show();
135+
<% } %>
136+
<% } %>
137+
}
138+
});
139+
64140
sdk.callbacks.push(onIdentityUpdated);
65141
});
66142
</script>
67143
</head>
68144
<body>
145+
<%- include('intro.html'); -%>
146+
<p>
147+
<strong>Note:</strong> This is a <em>test-only</em> integration environment—not for production
148+
use. It does not perform real user authentication or generate production-level tokens. Do not
149+
use real user data on this page.
150+
</p>
69151
<div id="googleAdContainer" style="display: none">
70152
<div id="mainContainer">
71153
<div id="content">
@@ -77,17 +159,8 @@
77159
</div>
78160
<button id="playButton">Play</button>
79161
<script type="text/javascript" src="//imasdk.googleapis.com/js/sdkloader/ima3.js"></script>
80-
<script async src="<%- secureSignalsSdkUrl %>"></script>
81-
<script async src="<%- uidJsSdkUrl %>"></script>
82-
<script async src="https://securepubads.g.doubleclick.net/tag/js/gpt.js"></script>
83162
<script type="text/javascript" src="ads.js"></script>
84163
</div>
85-
<%- include('intro.html'); -%>
86-
<p>
87-
<strong>Note:</strong> This is a <em>test-only</em> integration environment—not for production
88-
use. It does not perform real user authentication or generate production-level tokens. Do not
89-
use real user data on this page.
90-
</p>
91164
<table id="uid2_state">
92165
<tr>
93166
<td class="label">Ready for Targeted Advertising:</td>
@@ -109,7 +182,18 @@
109182
<td class="label"><%- identityName %> Identity Callback State:</td>
110183
<td class="value"><pre id="identity_state"></pre></td>
111184
</tr>
185+
<tr>
186+
<td class="label">Secure Signals Loaded?</td>
187+
<td class="value"><pre id="secure_signals_loaded"></pre></td>
188+
</tr>
189+
<tr>
190+
<td class="label">Secure Signals Value:</td>
191+
<td class="value"><pre id="secure_signals_value"></pre></td>
192+
</tr>
112193
</table>
194+
<div id="optout_banner" style="display: none; border: 3px solid #ffc107; padding: 15px; margin: 20px 0;">
195+
<p style="margin: 0;">The email address you entered has opted out of <%- identityName %>.</p>
196+
</div>
113197
<div id="login_form" style="display: none" class="form">
114198
<form action="/login" method="POST">
115199
<div class="email_prompt">
@@ -121,13 +205,14 @@
121205
style="border-style: none"
122206
/>
123207
</div>
124-
<div><input type="submit" value="Generate <%- identityName %>" class="button" id="login" /></div>
208+
<div><input type="submit" value="Generate <%- identityName %>" class="button" /></div>
125209
</form>
126210
</div>
127211
<div id="logout_form" style="display: none" class="form">
128-
<form>
129-
<button type="button" class="button" id="logout">Clear <%- identityName %></button>
130-
</form>
212+
<button type="button" class="button" id="logout">Clear <%- identityName %></button>
213+
</div>
214+
<div id="optout_form" style="display: none" class="form">
215+
<button type="button" class="button" id="try_another">Try Another Email</button>
131216
</div>
132217
</body>
133218
</html>

web-integrations/google-secure-signals/client-server/views/login.html

Lines changed: 0 additions & 39 deletions
This file was deleted.

web-integrations/google-secure-signals/client-server/views/optout.html

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)