{"id":27486,"date":"2024-05-21T12:23:00","date_gmt":"2024-05-21T16:23:00","guid":{"rendered":"https:\/\/plumrocket.com\/docs\/?p=27486"},"modified":"2025-05-29T11:40:17","modified_gmt":"2025-05-29T15:40:17","slug":"how-to-integrate-google-analytics-ga-with-magento-2-cookie-consent-extension","status":"publish","type":"post","link":"https:\/\/plumrocket.com\/docs\/magento-cookie-consent\/v2\/devguide\/ga-integration","title":{"rendered":"How to Integrate Google Analytics (GA) with Magento 2 Cookie Consent Extension"},"content":{"rendered":"\n<p>This guide will show you three options to easily set up Google Analytics with <a href=\"\/magento-cookie-consent\">Magento 2 Cookie Consent extension<\/a> and enable it for a particular cookie category.<\/p>\n\n\n\n<h3 id=\"h-built-in-google-analytics-support\">Built-in Google Analytics support<\/h3>\n\n\n\n<p>The module partially supports a built-in Magento Google Analytics module, namely:<\/p>\n\n\n\n<ul><li>Google Analytics executes when a user agrees to all cookie categories by clicking the \u201cAllow All\u201d button.<\/li><li>If you enable the Cookie Consent extension <a href=\"\/docs\/magento-cookie-consent\/v1\/configuration?preview_id=16436&amp;preview_nonce=23c7a5a2ee#cookie-consent-geo-targeting\" target=\"_blank\" aria-label=\"undefined (opens in a new tab)\" rel=\"noreferrer noopener\">only for certain countries<\/a>, Google Analytics executes after a customer allows cookies. For other countries, the Google Analytics code will work without restrictions.<\/li><\/ul>\n\n\n\n<h2 id=\"h-how-to-enable-google-analytics-for-a-particular-cookie-category\">How to enable Google Analytics for a particular cookie category<\/h2>\n\n\n\n<p>In order to block Google Analytics until a customer accepts cookies, you should use one of the following solutions:<\/p>\n\n\n\n<h3 id=\"h-option-1-using-google-tag-manager\">Option 1. Using Google Tag Manager<\/h3>\n\n\n\n<p>First, add your Google Analytics to <a href=\"https:\/\/tagmanager.google.com\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Google Tag Manager<\/a>. Then go to your Magento Admin Panel and configure <a aria-label=\"undefined (opens in a new tab)\" href=\"\/docs\/magento-cookie-consent\/v1\/configuration#google-tag-manager-settings\" target=\"_blank\" rel=\"noreferrer noopener\">Google Tag Manager<\/a> in Cookie Consent Extension.<\/p>\n\n\n\n<h3 id=\"h-option-2-using-cookie-category-interface\">Option 2. Using Cookie Category Interface<\/h3>\n\n\n\n<p>Go to the <a aria-label=\"undefined (opens in a new tab)\" href=\"\/docs\/magento-cookie-consent\/v1\/configuration#edit-cookie-category\" target=\"_blank\" rel=\"noreferrer noopener\">Edit Cookie Category page<\/a> in the backend and insert your Google Analytics tag in the <a aria-label=\"undefined (opens in a new tab)\" href=\"\/docs\/magento-cookie-consent\/v1\/configuration#category-head-scripts\" target=\"_blank\" rel=\"noreferrer noopener\">Head Scripts<\/a> field.<\/p>\n\n\n\n<h3 id=\"h-option-3-customizing-ga-built-in-support\">Option 3. Customizing GA Built-In Support<\/h3>\n\n\n\n<p><strong>Step 1<\/strong>. Create the following js file in your theme folder.<\/p>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-bash\">app\/design\/&lt;Vendor>\/&lt;theme>\/Magento_GoogleAnalytics\/web\/js\/google-analytics.js<\/code><\/pre><\/div>\n\n\n\n<p><strong>Step 2<\/strong>. Paste the following code into the created file.<\/p>\n\n\n\n<p>In the example below, Google Analytics executes when a customer accepts the Statistics cookie category. If you want Google Analytics to execute when a customer accepts another cookie category, change the <strong>&#8216;statistics&#8217;<\/strong> key in the following code to the relevant <a href=\"\/docs\/magento-cookie-consent\/v1\/configuration#cookie-category-key\" target=\"_blank\" aria-label=\"undefined (opens in a new tab)\" rel=\"noreferrer noopener\">category key<\/a>.<\/p>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-javascript\">\/**\n * Copyright \u00a9 Magento, Inc. All rights reserved.\n * See COPYING.txt for license details.\n *\/\n\/* jscs:disable *\/\n\/* eslint-disable *\/\ndefine([\n    'jquery',\n    'prCookieConsentApi',\n    'domReady!'\n], function ($, prCookieConsentApi) {\n    'use strict';\n\n    \/**\n     * @param {Object} config\n     *\/\n    return function (config) {\n        if (prCookieConsentApi.isAllowedCategory('statistics')) {\n            (function (i, s, o, g, r, a, m) {\n                i.GoogleAnalyticsObject = r;\n                i[r] = i[r] || function () {\n                    (i[r].q = i[r].q || []).push(arguments)\n                }, i[r].l = 1 * new Date();\n                a = s.createElement(o),\n                    m = s.getElementsByTagName(o)[0];\n                a.async = 1;\n                a.src = g;\n                m.parentNode.insertBefore(a, m)\n            })(window, document, 'script', '\/\/www.google-analytics.com\/analytics.js', 'ga');\n\n            \/\/ Process page info\n            ga('create', config.pageTrackingData.accountId, 'auto');\n\n            if (config.pageTrackingData.isAnonymizedIpActive) {\n                ga('set', 'anonymizeIp', true);\n            }\n\n            \/\/ Process orders data\n            if (config.ordersTrackingData.hasOwnProperty('currency')) {\n                ga('require', 'ec', 'ec.js');\n\n                ga('set', 'currencyCode', config.ordersTrackingData.currency);\n\n                \/\/ Collect product data for GA\n                if (config.ordersTrackingData.products) {\n                    $.each(config.ordersTrackingData.products, function (index, value) {\n                        ga('ec:addProduct', value);\n                    });\n                }\n\n                \/\/ Collect orders data for GA\n                if (config.ordersTrackingData.orders) {\n                    $.each(config.ordersTrackingData.orders, function (index, value) {\n                        ga('ec:setAction', 'purchase', value);\n                    });\n                }\n\n                ga('send', 'pageview');\n            } else {\n                \/\/ Process Data if not orders\n                ga('send', 'pageview' + config.pageTrackingData.optPageUrl);\n            }\n        }\n    }\n});<\/code><\/pre><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This guide will show you three options to easily set up Google Analytics with Magento 2 Cookie Consent extension and enable it for a particular cookie category. Built-in Google Analytics support The module partially supports a built-in Magento Google Analytics module, namely: Google Analytics executes when a user agrees to all cookie categories by clicking &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/plumrocket.com\/docs\/magento-cookie-consent\/v2\/devguide\/ga-integration\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Integrate Google Analytics (GA) with Magento 2 Cookie Consent Extension&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[383],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v17.2 (Yoast SEO v17.2) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to Integrate Google Analytics (GA) with Magento 2 Cookie Consent Extension - Plumrocket Documentation<\/title>\n<meta name=\"description\" content=\"This guide will show you three options to easily set up Google Analytics with Magento 2 Cookie Consent extension and enable it for a particular cookie\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/plumrocket.com\/docs\/magento-cookie-consent-pro\/v1\/devguide\/ga-integration\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Integrate Google Analytics (GA) with Magento 2 Cookie Consent Extension\" \/>\n<meta property=\"og:description\" content=\"This guide will show you three options to easily set up Google Analytics with Magento 2 Cookie Consent extension and enable it for a particular cookie\" \/>\n<meta property=\"og:url\" content=\"https:\/\/plumrocket.com\/docs\/magento-cookie-consent-pro\/v1\/devguide\/ga-integration\" \/>\n<meta property=\"og:site_name\" content=\"Plumrocket Documentation\" \/>\n<meta property=\"article:published_time\" content=\"2024-05-21T16:23:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-05-29T15:40:17+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Plumrocket\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to Integrate Google Analytics (GA) with Magento 2 Cookie Consent Extension - Plumrocket Documentation","description":"This guide will show you three options to easily set up Google Analytics with Magento 2 Cookie Consent extension and enable it for a particular cookie","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/plumrocket.com\/docs\/magento-cookie-consent-pro\/v1\/devguide\/ga-integration","og_locale":"en_US","og_type":"article","og_title":"How to Integrate Google Analytics (GA) with Magento 2 Cookie Consent Extension","og_description":"This guide will show you three options to easily set up Google Analytics with Magento 2 Cookie Consent extension and enable it for a particular cookie","og_url":"https:\/\/plumrocket.com\/docs\/magento-cookie-consent-pro\/v1\/devguide\/ga-integration","og_site_name":"Plumrocket Documentation","article_published_time":"2024-05-21T16:23:00+00:00","article_modified_time":"2025-05-29T15:40:17+00:00","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Plumrocket","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"https:\/\/plumrocket.com\/docs\/#website","url":"https:\/\/plumrocket.com\/docs\/","name":"Plumrocket Documentation","description":"Extensions docs, troubleshootings etc.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/plumrocket.com\/docs\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/plumrocket.com\/docs\/magento-cookie-consent-pro\/v1\/devguide\/ga-integration#webpage","url":"https:\/\/plumrocket.com\/docs\/magento-cookie-consent-pro\/v1\/devguide\/ga-integration","name":"How to Integrate Google Analytics (GA) with Magento 2 Cookie Consent Extension - Plumrocket Documentation","isPartOf":{"@id":"https:\/\/plumrocket.com\/docs\/#website"},"datePublished":"2024-05-21T16:23:00+00:00","dateModified":"2025-05-29T15:40:17+00:00","author":{"@id":"https:\/\/plumrocket.com\/docs\/#\/schema\/person\/c96fccdb89342ae6804272265723eca8"},"description":"This guide will show you three options to easily set up Google Analytics with Magento 2 Cookie Consent extension and enable it for a particular cookie","breadcrumb":{"@id":"https:\/\/plumrocket.com\/docs\/magento-cookie-consent-pro\/v1\/devguide\/ga-integration#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/plumrocket.com\/docs\/magento-cookie-consent-pro\/v1\/devguide\/ga-integration"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/plumrocket.com\/docs\/magento-cookie-consent-pro\/v1\/devguide\/ga-integration#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Magento 2 Extensions","item":"https:\/\/plumrocket.com\/magento-extensions"},{"@type":"ListItem","position":2,"name":"Magento 2 Cookie Consent","item":"https:\/\/plumrocket.com\/magento-cookie-consent"},{"@type":"ListItem","position":3,"name":"Documentation","item":"https:\/\/plumrocket.com\/docs\/magento-cookie-consent"},{"@type":"ListItem","position":4,"name":"v2","item":"https:\/\/plumrocket.com\/docs\/magento-cookie-consent\/v2"},{"@type":"ListItem","position":5,"name":"Developer Guide","item":"https:\/\/plumrocket.com\/docs\/magento-cookie-consent\/v2\/devguide"},{"@type":"ListItem","position":6,"name":"How to Integrate Google Analytics (GA) with Magento 2 Cookie Consent Extension"}]},{"@type":"Person","@id":"https:\/\/plumrocket.com\/docs\/#\/schema\/person\/c96fccdb89342ae6804272265723eca8","name":"Plumrocket","image":{"@type":"ImageObject","@id":"https:\/\/plumrocket.com\/docs\/#personlogo","inLanguage":"en-US","url":"https:\/\/secure.gravatar.com\/avatar\/10c44aa45aab391250913d982e552e53?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/10c44aa45aab391250913d982e552e53?s=96&d=mm&r=g","caption":"Plumrocket"}}]}},"_links":{"self":[{"href":"https:\/\/plumrocket.com\/docs\/wp-json\/wp\/v2\/posts\/27486"}],"collection":[{"href":"https:\/\/plumrocket.com\/docs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/plumrocket.com\/docs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/plumrocket.com\/docs\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/plumrocket.com\/docs\/wp-json\/wp\/v2\/comments?post=27486"}],"version-history":[{"count":1,"href":"https:\/\/plumrocket.com\/docs\/wp-json\/wp\/v2\/posts\/27486\/revisions"}],"predecessor-version":[{"id":27487,"href":"https:\/\/plumrocket.com\/docs\/wp-json\/wp\/v2\/posts\/27486\/revisions\/27487"}],"wp:attachment":[{"href":"https:\/\/plumrocket.com\/docs\/wp-json\/wp\/v2\/media?parent=27486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/plumrocket.com\/docs\/wp-json\/wp\/v2\/categories?post=27486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/plumrocket.com\/docs\/wp-json\/wp\/v2\/tags?post=27486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}