{"id":28385,"date":"2025-05-29T11:36:57","date_gmt":"2025-05-29T15:36:57","guid":{"rendered":"https:\/\/plumrocket.com\/docs\/?p=28385"},"modified":"2025-05-29T11:36:59","modified_gmt":"2025-05-29T15:36:59","slug":"how-to-block-your-javascript-code-until-a-customer-accepts-cookies-3","status":"publish","type":"post","link":"https:\/\/plumrocket.com\/docs\/magento-cookie-consent-pro\/v1\/devguide\/block-js-code","title":{"rendered":"How to Block Your JavaScript Code Until a Customer Accepts Cookies"},"content":{"rendered":"\n<p>In this guide, you can learn how to block a specific JavaScript code until a user accepts cookies.<\/p>\n\n\n\n<h2 id=\"h-option-1-recommended-block-js-using-tag-attribute\">Option 1 (recommended): Block JS Using Tag Attribute<\/h2>\n\n\n\n<p>If you need to run the JS code only when a user agreed to the Marketing cookie category, you need to add a specific <code>type<\/code> attribute to the <code>script<\/code> tag. <\/p>\n\n\n\n<p><strong>How does it work?<\/strong> Generally, you can include any JS in the <code>&lt;script&gt;<\/code> tag, however, it&#8217;s all about its <code>type<\/code> attribute. A browser recognizes <code>&lt;script&gt;<\/code> and <code>&lt;script type=\"text\/javascript\"&gt;<\/code> tags as JavaScript and can run it, but when you change the tag to a custom one, it becomes unknown and cannot be run. This is where the Cookie Consent extension comes into play. You should add the specific custom <code>type<\/code> attribute to the <code>script<\/code> tag (e.g. <code>&lt;script type=\"pr_cookie_category\/marketing\"&gt;<\/code>), so that the plugin can change it to a recognizable <code>&lt;script type=\"text\/javascript\"&gt;<\/code> only after a user allows the Marketing cookie category. As a result, you can run only the necessary scripts.<\/p>\n\n\n\n<p>For example, let&#8217;s add the <code>\"pr_cookie_category\/marketing\"<\/code> type to the following inline JS code:<\/p>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-markup\">&lt;script>\n\talert('Marketing script was executed');\n&lt;\/script><\/code><\/pre><\/div>\n\n\n\n<p>After adding the <code>type<\/code> attribute, the code will look as follows:<\/p>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-markup\">&lt;script type=\"pr_cookie_category\/marketing\">\n\talert('Marketing script was executed');\n&lt;\/script><\/code><\/pre><\/div>\n\n\n\n<p>In the case of external JavaScript, add the &#8220;<code>pr_cookie_category\/marketing<\/code>&#8221; type in the same way to the following code:<\/p>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-markup\">&lt;script src=\"https:\/\/example.com\/file.js\">&lt;\/script><\/code><\/pre><\/div>\n\n\n\n<p>After adding the <code>type<\/code> attribute, the code will look as follows:<\/p>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-markup\">&lt;script type=\"pr_cookie_category\/marketing\" src=\"https:\/\/example.com\/file.js\">&lt;\/script><\/code><\/pre><\/div>\n\n\n\n<p>The extension supports the following <code>type<\/code> values: <\/p>\n\n\n\n<ul><li><code>pr_cookie_category\/all<\/code> &#8211; a script with this attribute will be run if a user agrees to all cookies.<\/li><li><code>pr_cookie_category\/{{category_key}}<\/code> &#8211; a script with this attribute will be run if a user agrees to the specific cookie category or allows all cookies. <\/li><\/ul>\n\n\n\n<h2 id=\"h-option-2-partial-blocking-block-js-using-our-javascript-model\">Option 2 (partial blocking): Block JS Using Our JavaScript Model<\/h2>\n\n\n\n<p>Wrap your javascript code as in the example below: <\/p>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-javascript\">prCookieConsentApi.userScript.execute(function() {\n      \/\/ your code goes here ...\n}, 'marketing');<\/code><\/pre><\/div>\n\n\n\n<p>&#8220;<strong>marketing<\/strong>&#8221; is the key of the Marketing category. The script will be executed if a customer allows the marketing category. Please, learn <a href=\"\/docs\/magento-cookie-consent\/v1\/configuration#cookie-category-key\">how to find a category key<\/a>.<\/p>\n\n\n\n<p>To find out more about the &#8220;prCookieConsentApi&#8221; JS model, go to the following file:<\/p>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-bash\">Plumrocket\/CookieConsent\/view\/frontend\/templates\/restriction_config.phtml<\/code><\/pre><\/div>\n\n\n\n<h2 id=\"h-option-3-advanced-block-js-using-pr-cookie-consent-cookie\">Option 3 (advanced): Block JS Using &#8220;pr-cookie-consent&#8221; Cookie<\/h2>\n\n\n\n<p>If you are not able to use our js model, you can write your own solution based on the&nbsp;&#8220;<strong>pr-cookie-consent<\/strong>&#8221;&nbsp;cookie.<\/p>\n\n\n\n<p><strong>A &#8220;<strong>pr-cookie-consent&#8221;<\/strong><\/strong> <strong>value is<\/strong> an array that includes categories allowed by a customer. The value is a URL encoded JSON string. For instance, if a customer allows cookies from the Statistics <a href=\"\/docs\/magento-cookie-consent\/v1\/configuration?preview_id=16436&amp;preview_nonce=23c7a5a2ee#cooking-categories\" target=\"_blank\" aria-label=\"undefined (opens in a new tab)\" rel=\"noreferrer noopener\">category<\/a>, the consent will be recorded as [%22statistics%22] in the<strong> <\/strong>&#8220;pr-cookie-consent&#8221;<strong> <\/strong>array.<\/p>\n\n\n\n<p>In the list below, you can see the <strong>examples of possible &#8220;pr-cookie-consent&#8221; cookie values<\/strong> (for illustrative purposes, the values are URL decoded):<\/p>\n\n\n\n<ul><li>[&#8220;statistics&#8221;] &#8211; a user allowed a Statistics category only.<\/li><li>[&#8220;preferences&#8221;,&#8221;statistics&#8221;]&nbsp; &#8211; a user allowed Preferences and Statistics categories.<\/li><li>[&#8220;all&#8221;]&nbsp; &#8211; a user allowed all cookies.<\/li><li>[] &#8211; a user declined all cookies or disabled all categories in a <a href=\"#cookie-settings-panel\">Cookie Settings Panel<\/a>.<\/li><\/ul>\n\n\n\n<p><strong>Please, note: <\/strong><\/p>\n\n\n\n<ul><li>If a user has not allowed cookies yet, a &#8220;pr-cookie-consent&#8221; cookie is not created.<\/li><li>All but the <a href=\"\/docs\/magento-cookie-consent\/v1\/configuration#essential-cookie-category\">essential categories<\/a> are stored in the &#8220;pr-cookie-consent&#8221; cookie. Since the Essential categories are required and users are not able to decline them, these categories are not recorded in the cookie.<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, you can learn how to block a specific JavaScript code until a user accepts cookies. Option 1 (recommended): Block JS Using Tag Attribute If you need to run the JS code only when a user agreed to the Marketing cookie category, you need to add a specific type attribute to the script &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/plumrocket.com\/docs\/magento-cookie-consent-pro\/v1\/devguide\/block-js-code\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Block Your JavaScript Code Until a Customer Accepts Cookies&#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":[410],"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 Block Your JavaScript Code Until a Customer Accepts Cookies - Plumrocket Documentation<\/title>\n<meta name=\"description\" content=\"In this guide, you can learn how to block a specific JavaScript code until a user accepts cookies. Option 1 (recommended): Block JS Using Tag Attribute If\" \/>\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\/block-js-code\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Block Your JavaScript Code Until a Customer Accepts Cookies\" \/>\n<meta property=\"og:description\" content=\"In this guide, you can learn how to block a specific JavaScript code until a user accepts cookies. Option 1 (recommended): Block JS Using Tag Attribute If\" \/>\n<meta property=\"og:url\" content=\"https:\/\/plumrocket.com\/docs\/magento-cookie-consent-pro\/v1\/devguide\/block-js-code\" \/>\n<meta property=\"og:site_name\" content=\"Plumrocket Documentation\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-29T15:36:57+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-05-29T15:36:59+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=\"3 minutes\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to Block Your JavaScript Code Until a Customer Accepts Cookies - Plumrocket Documentation","description":"In this guide, you can learn how to block a specific JavaScript code until a user accepts cookies. Option 1 (recommended): Block JS Using Tag Attribute If","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\/block-js-code","og_locale":"en_US","og_type":"article","og_title":"How to Block Your JavaScript Code Until a Customer Accepts Cookies","og_description":"In this guide, you can learn how to block a specific JavaScript code until a user accepts cookies. Option 1 (recommended): Block JS Using Tag Attribute If","og_url":"https:\/\/plumrocket.com\/docs\/magento-cookie-consent-pro\/v1\/devguide\/block-js-code","og_site_name":"Plumrocket Documentation","article_published_time":"2025-05-29T15:36:57+00:00","article_modified_time":"2025-05-29T15:36:59+00:00","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Plumrocket","Est. reading time":"3 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\/block-js-code#webpage","url":"https:\/\/plumrocket.com\/docs\/magento-cookie-consent-pro\/v1\/devguide\/block-js-code","name":"How to Block Your JavaScript Code Until a Customer Accepts Cookies - Plumrocket Documentation","isPartOf":{"@id":"https:\/\/plumrocket.com\/docs\/#website"},"datePublished":"2025-05-29T15:36:57+00:00","dateModified":"2025-05-29T15:36:59+00:00","author":{"@id":"https:\/\/plumrocket.com\/docs\/#\/schema\/person\/c96fccdb89342ae6804272265723eca8"},"description":"In this guide, you can learn how to block a specific JavaScript code until a user accepts cookies. Option 1 (recommended): Block JS Using Tag Attribute If","breadcrumb":{"@id":"https:\/\/plumrocket.com\/docs\/magento-cookie-consent-pro\/v1\/devguide\/block-js-code#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/plumrocket.com\/docs\/magento-cookie-consent-pro\/v1\/devguide\/block-js-code"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/plumrocket.com\/docs\/magento-cookie-consent-pro\/v1\/devguide\/block-js-code#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":"v1","item":"https:\/\/plumrocket.com\/docs\/magento-cookie-consent-pro\/v1"},{"@type":"ListItem","position":5,"name":"Developer Guide","item":"https:\/\/plumrocket.com\/docs\/magento-cookie-consent-pro\/v1\/devguide"},{"@type":"ListItem","position":6,"name":"How to Block Your JavaScript Code Until a Customer Accepts Cookies"}]},{"@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\/28385"}],"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=28385"}],"version-history":[{"count":1,"href":"https:\/\/plumrocket.com\/docs\/wp-json\/wp\/v2\/posts\/28385\/revisions"}],"predecessor-version":[{"id":28386,"href":"https:\/\/plumrocket.com\/docs\/wp-json\/wp\/v2\/posts\/28385\/revisions\/28386"}],"wp:attachment":[{"href":"https:\/\/plumrocket.com\/docs\/wp-json\/wp\/v2\/media?parent=28385"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/plumrocket.com\/docs\/wp-json\/wp\/v2\/categories?post=28385"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/plumrocket.com\/docs\/wp-json\/wp\/v2\/tags?post=28385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}