Cookie Compliance and Google Analytics
From the EU Cookie Compliance module's project page on drupal.org:

If you want to conditionally set cookies in your module, there is a
JavaScript function provided that returns TRUE if the user has given
their consent:
Drupal.eu_cookie_compliance.hasAgreed()
For example, if you want to disable Google Analytics, you could do the
following: http://drupal.org/node/1648286#comment-6145800
Another example of how it can be used with Google Analytics is
provided by dagomar in this comment.
Here is another example showing how to set cookies after user clicks
the accept button: http://drupal.org/node/1624876#comment-6111688

I've personally used this to prevent third-party code snippets from loading until after compliance is granted, by making a custom module and including the call to hasAgreed() in my module's JavaScript behaviors.
But as the first example above shows, a custom module is not necessary for Google Analytics in particular. You can simply add the following code to the Advanced Settings-&gt;Custom JS in Google Analytics:
if (!Drupal.eu_cookie_compliance.hasAgreed()){
window['ga-disable-UA-xxxx-1'] = true;
}

EDIT (2020-02-23):
It seems that you want to enforce extremely strict GDPR compliance, and prevent the cookies from being loaded at all unless consent has been granted.
You can do this with a custom module, leveraging the same hasAgreed() JavaScript behavior provided by the EU Cookie Compilance module.
First you need to remove the code snippet from your site completely and replace it with a placeholder.
If it's just one script in your HTML head, you might just edit html.html.twig.
If there are multiple scripts you need to prevent from running, it will be more work.
In my case, I replaced dozens of different third-party forms with an empty &lt;div&gt; element:
&lt;div id=&quot;1234&quot; class=&quot;thirdPartyFormWrapper&quot;&gt;&lt;/div&gt;

My custom JavaScript behavior looks for all the DOM elements with that class:
document.getElementsByClassName('thirdPartyFormWrapper');

The 4-digit number in the div ID corresponds to the unique third-party form ID for each form.
If consent has not been granted (or if the EU Cookie Compliance module is not installed or enabled, and hasAgreed() is not available) then my custom module uses the 4-digit ID to look up substitute content (saying, e.g., &quot;Please accept cookies to view the Yoyodyne, Inc. Gnomovision Mailing List Subscription Form,&quot; or whatever that particular form is.)
If consent has been granted, then my custom module uses the 4-digit ID to inject the JavaScript snippet for each of the third-party forms into the placeholder &lt;div&gt;.
Once the form snippet is injected, you can figure out how to handle the cookies. If consent is withheld, neither snippets nor cookies will load.
You can also use this technique to handle cookies for extremely strict GDPR compliance, following this pattern:
/**
 * Cookie handling for strict GDPR compliance
 */
  Drupal.behaviors.customCookieConsent = {
    attach: function (context, settings) {
      if (!Drupal.eu_cookie_compliance || !Drupal.eu_cookie_compliance.hasAgreed()) {
        // Bail out if cookie consent not given
        return;
      }
      // If cookie consent is given, set cookie (unless already set)
      if (!$.cookie('customConsentTime')){
        var consentTime = new Date();
        $.cookie('customConsentTime', consentTime.toISOString(), {expires: 3650});
      }
    },
  };

This approach is a bit more work, but it will prevent scripts and cookies from being loaded at all before consent is granted.
Finally, I should note that based on your comments here and on Drupal.org, I think you should do some research on &quot;Drupal Behaviors&quot; for your JavaScript. This will help you to avoid the &quot;Uncaught ReferenceError: Drupal is not defined&quot; message you have been seeing.