Issue
- When Google Analytics is enabled in Liferay cookies like _gid and _ga are stored in browser session. How can we avoid storing these cookies?
Environment
- Liferay DXP (any version)
Resolution
- In 2024.Q1, Liferay has released an easy-to-use mechanism to allow third-party cookie blocking. See Managing Third-Party Cookies.
- This mechanism will be progressively rolled out within the Product to manage third-party cookies as needed, such as those introduced by GA.
- In the mean time, you can avoid storing cookies generated by Google Analytics through a custom development.
NOTE: The following resolution requires customization and should only be implemented at the discretion of your team. Liferay Support will not be able to assist with designing or implementing customizations.
- An example, based on the information provided by Google would be to override
top_js.jspf
file adding the configuration {storage:none} when Google Analytics's create command is triggered, so we'd need something like:ga('create', '<%= HtmlUtil.escapeJS(googleAnalyticsId) %>', {
'storage': 'none'
});
ga('send', 'pageview'); - Another approach for Liferay 7.4+ would be to use Global JS Client Extension. Steps:
- Determine the right type of cookie to check consent for.
- Make an appropriate call to
Liferay.Util.Cookie
. Use the following snippet as an example for your Client Extension (it takes into account cases where SPA is enabled):if (Liferay && Liferay.SPA) {
Liferay.once('endNavigate', function() {
const myCookieValue = Liferay.Util.Cookie.get("CONSENT_TYPE_PERSONALIZATION", Liferay.Util.Cookie.TYPES.NECESSARY);
// TODO check myCookieValue and load your script if true
});
}- Note: if you are using DXP 2024.Q1 or a newer version, you can leverage the
data-third-party-cookie
attribute as explained in Managing Third-Party Cookies.
- Note: if you are using DXP 2024.Q1 or a newer version, you can leverage the
- Then Disable GA in the product itself, in favor of this particular development.
- Deploy and add you Client Extension to all and every page you want to control.