Skip to content

Commit

Permalink
LPD-19859, LPD-21209 Update top_head dynamic include to conditionally…
Browse files Browse the repository at this point in the history
… include the JS SDK based on the Cookie Configuration, and support OneTrust cookie manager
  • Loading branch information
interaminense authored and brianchandotcom committed Mar 27, 2024
1 parent 69463cf commit 16c2500
Show file tree
Hide file tree
Showing 2 changed files with 222 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,66 +18,239 @@
'<%= (String)request.getAttribute(AnalyticsWebKeys.ANALYTICS_CLIENT_CHANNEL_ID) %>';
var analyticsClientGroupIds = <%= (String)request.getAttribute(AnalyticsWebKeys.ANALYTICS_CLIENT_GROUP_IDS) %>;
var analyticsCookiesConsentMode = <%= (boolean)request.getAttribute(AnalyticsWebKeys.ANALYTICS_COOKIES_EXPLICIT_CONSENT_MODE) %>;
var analyticsFeatureFlagEnabled = <%= FeatureFlagManagerUtil.isEnabled("LPD-10588") %>;

var cookieManagers = {
'cookie.liferay': {
actions: {
getItem: (key) => {
var data;

try {
var cookie = Liferay.Util.Cookie.get(
key,
Liferay.Util.Cookie.TYPES.PERFORMANCE
);

data = JSON.parse(decodeURIComponent(cookie));
}
catch (error) {
return;
}

return data;
},
getItemFromLocalStorage: (key) => {
let data;

try {
const item = Liferay.Util.LocalStorage.getItem(
key,
Liferay.Util.LocalStorage.TYPES.PERFORMANCE
);
data = JSON.parse(item);
}
catch (error) {
return;
}

return data;
},
removeItem: (key) => {
Liferay.Util.Cookie.remove(
key,
Liferay.Util.Cookie.TYPES.PERFORMANCE
);
},
setItem: (key, value, encode = true) => {
var expires = new Date();

expires.setDate(expires.getDate() + 365);

try {
var jsonStr = JSON.stringify(value);
var data = encode ? encodeURIComponent(jsonStr) : jsonStr;

Liferay.Util.Cookie.set(
key,
data,
Liferay.Util.Cookie.TYPES.PERFORMANCE,
{
expires,
secure: true,
}
);
}
catch (error) {
return;
}
},
},
checkConsent: () => {
var performanceCookieEnabled = Liferay.Util.Cookie.get(
Liferay.Util.Cookie.TYPES.PERFORMANCE
);

return performanceCookieEnabled === 'true';
},
enabled: () => {
return Promise.resolve(
analyticsFeatureFlagEnabled && analyticsCookiesConsentMode
);
},
onConsentChange: (callbackFn) => {
Liferay.on('cookieBannerSetCookie', callbackFn);
},
},
'cookie.onetrust': {
enabled: () => {
if (!window.OneTrustStub) {
return Promise.resolve(false);
}

return new Promise((resolve, reject) => {
var startTime = Date.now();

var checkObject = () => {
if (window['OneTrust']) {
resolve(window['OneTrust']);
}
else if (Date.now() - startTime >= 5000) {
reject();
}
else {
setTimeout(checkObject, 100);
}
};

checkObject();
})
.then(() => {
return Promise.resolve(true);
})
.catch(() => {
return Promise.resolve(false);
});
},
checkConsent: () => {
var OptanonActiveGroups = window.OptanonActiveGroups;

return OptanonActiveGroups && OptanonActiveGroups.includes('C0002');
},
onConsentChange: (callbackFn) => {
var OneTrust = window.OneTrust;

OneTrust.OnConsentChanged(callbackFn);
},
},
};
</aui:script>

<aui:script id="liferayAnalyticsScript" senna="permanent" type="text/javascript">
(function (u, c, a, m, o, l) {
o = 'script';
l = document;
a = l.createElement(o);
m = l.getElementsByTagName(o)[0];
a.async = 1;
a.src = u;
a.onload = c;
m.parentNode.insertBefore(a, m);
})('https://analytics-js-cdn.liferay.com', () => {
var config = <%= (String)request.getAttribute(AnalyticsWebKeys.ANALYTICS_CLIENT_CONFIG) %>;

var dxpMiddleware = function (request) {
request.context.canonicalUrl = themeDisplay.getCanonicalURL();
request.context.channelId = analyticsClientChannelId;
request.context.groupId = themeDisplay.getScopeGroupIdOrLiveGroupId();

return request;
};

Analytics.create(config, [dxpMiddleware]);

if (themeDisplay.isSignedIn()) {
Analytics.setIdentity({
email: themeDisplay.getUserEmailAddress(),
name: themeDisplay.getUserName(),
});
}
var allPromises = Object.keys(cookieManagers).map((key) =>
cookieManagers[key].enabled()
);

Promise.all(allPromises).then((result) => {
var selectedIndex = result.findIndex((enabled) => enabled);
var selectedCookieManager = Object.values(cookieManagers)[selectedIndex];

function <portlet:namespace />initializeAnalyticsSDK() {
(function (u, c, a, m, o, l) {
o = 'script';
l = document;
a = l.createElement(o);
m = l.getElementsByTagName(o)[0];
a.async = 1;
a.src = u;
a.onload = c;
m.parentNode.insertBefore(a, m);
})('https://analytics-js-cdn.liferay.com', () => {
var config = <%= (String)request.getAttribute(AnalyticsWebKeys.ANALYTICS_CLIENT_CONFIG) %>;

config.cookieManager = selectedCookieManager;

var dxpMiddleware = function (request) {
request.context.canonicalUrl = themeDisplay.getCanonicalURL();
request.context.channelId = analyticsClientChannelId;
request.context.groupId = themeDisplay.getScopeGroupIdOrLiveGroupId();

return request;
};

Analytics.create(config, [dxpMiddleware]);

if (themeDisplay.isSignedIn()) {
Analytics.setIdentity({
email: themeDisplay.getUserEmailAddress(),
name: themeDisplay.getUserName(),
});
}

runMiddlewares();
runMiddlewares();

Analytics.send('pageViewed', 'Page');
Analytics.send('pageViewed', 'Page');

<c:if test="<%= GetterUtil.getBoolean(PropsUtil.get(PropsKeys.JAVASCRIPT_SINGLE_PAGE_APPLICATION_ENABLED)) %>">
Liferay.on('endNavigate', (event) => {
Analytics.dispose();
<c:if test="<%= GetterUtil.getBoolean(PropsUtil.get(PropsKeys.JAVASCRIPT_SINGLE_PAGE_APPLICATION_ENABLED)) %>">
function <portlet:namespace />initializeAnalyticsSDKFromSPA() {
Liferay.on('endNavigate', (event) => {
Analytics.dispose();

var groupId = themeDisplay.getScopeGroupIdOrLiveGroupId();
var groupId = themeDisplay.getScopeGroupIdOrLiveGroupId();

if (
!themeDisplay.isControlPanel() &&
analyticsClientGroupIds.indexOf(groupId) >= 0
) {
Analytics.create(config, [dxpMiddleware]);
if (
!themeDisplay.isControlPanel() &&
analyticsClientGroupIds.indexOf(groupId) >= 0
) {
Analytics.create(config, [dxpMiddleware]);

if (themeDisplay.isSignedIn()) {
Analytics.setIdentity({
email: themeDisplay.getUserEmailAddress(),
name: themeDisplay.getUserName(),
if (themeDisplay.isSignedIn()) {
Analytics.setIdentity({
email: themeDisplay.getUserEmailAddress(),
name: themeDisplay.getUserName(),
});
}

runMiddlewares();

Analytics.send('pageViewed', 'Page', {
page: event.path,
});
}
});
}

runMiddlewares();
if (selectedCookieManager) {
selectedCookieManager.onConsentChange(() => {
if (selectedCookieManager.checkConsent()) {
<portlet:namespace />initializeAnalyticsSDKFromSPA();
}
});

Analytics.send('pageViewed', 'Page', {page: event.path});
if (selectedCookieManager.checkConsent()) {
<portlet:namespace />initializeAnalyticsSDKFromSPA();
}
}
else {
<portlet:namespace />initializeAnalyticsSDKFromSPA();
}
</c:if>
});
}

if (selectedCookieManager) {
selectedCookieManager.onConsentChange(() => {
if (selectedCookieManager.checkConsent()) {
<portlet:namespace />initializeAnalyticsSDK();
}
});
</c:if>

if (selectedCookieManager.checkConsent()) {
<portlet:namespace />initializeAnalyticsSDK();
}
}
else {
<portlet:namespace />initializeAnalyticsSDK();
}
});
</aui:script>
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>

<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %><%@
taglib uri="http://liferay.com/tld/frontend" prefix="liferay-frontend" %><%@
taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %><%@
taglib uri="http://liferay.com/tld/util" prefix="liferay-util" %>

<%@ page import="com.liferay.analytics.web.internal.constants.AnalyticsWebKeys" %><%@
page import="com.liferay.portal.kernel.feature.flag.FeatureFlagManagerUtil" %><%@
page import="com.liferay.portal.kernel.util.GetterUtil" %><%@
page import="com.liferay.portal.kernel.util.PropsKeys" %><%@
page import="com.liferay.portal.kernel.util.PropsUtil" %>
Expand Down

0 comments on commit 16c2500

Please sign in to comment.