Legacy Knowledge Base
Published Jul. 2, 2025

Avoid storing cookies generated by Google Analytics

Written By

Antonio Ortega

How To articles are not official guidelines or officially supported documentation. They are community-contributed content and may not always reflect the latest updates to Liferay DXP. We welcome your feedback to improve How To articles!

While we make every effort to ensure this Knowledge Base is accurate, it may not always reflect the most recent updates or official guidelines.We appreciate your understanding and encourage you to reach out with any feedback or concerns.

Legacy Article

You are viewing an article from our legacy "FastTrack" publication program, made available for informational purposes. Articles in this program were published without a requirement for independent editing or verification and are provided"as is" without guarantee.

Before using any information from this article, independently verify its suitability for your situation and project.

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.
    • 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.

Additional Information

Did this article resolve your issue ?

Legacy Knowledge Base