nystudio107 / craft-seomatic

SEOmatic facilitates modern SEO best practices & implementation for Craft CMS 3. It is a turnkey SEO system that is comprehensive, powerful, and flexible.
https://nystudio107.com/plugins/seomatic
Other
162 stars 68 forks source link

Change script sort order #1417

Closed maximilian-schwarz closed 4 months ago

maximilian-schwarz commented 4 months ago

Question

I want to change the sort order of scripts. Currently I would have to place a Javascript snippet before the Google Tag Manager script as described here. https://support.cookiebot.com/hc/en-us/articles/360016047000-Cookiebot-and-Google-Consent-Mode

Additional context

Currently i try to switch off the "Automatic Render Enabled". After switch off i defined the scripts like this:

{% set script = seomatic.script.render()|json_decode %}
<head>
  <script data-cookieconsent="ignore">
    window.dataLayer = window.dataLayer || [];
    function gtag() {
        dataLayer.push(arguments);
    }
    gtag("consent", "default", {
        ad_personalization: "denied",
        ad_storage: "denied",
        ad_user_data: "denied",
        analytics_storage: "denied",
        functionality_storage: "denied",
        personalization_storage: "denied",
        security_storage: "granted",
        wait_for_update: 500,
    });
    gtag("set", "ads_data_redaction", true);
    gtag("set", "url_passthrough", true);
  </script>

  {{ script.script|raw }}
  {{ seomatic.link.render() }}
  {{ seomatic.tag.render() }}
  {{ seomatic.title.render() }}
</head>
<body>
{{ script.bodyScript|raw }}
</body>

But if i try to integrate the scripts manually, sometimes the scripts (output from {{ script.script|raw }}) is outside of the <head></head> tags. Is this a bug? Maybe there is a simple solution to add the snippet before the google tag manager.

Thanks for your help :)

khalwat commented 4 months ago

There's no bug that I'm aware of, it should render things exactly where you tell it to.

However, an easier way to do this would probably be to leave Automatic Rendering on, and just modify the GA script listed in SEOmatic -> Tracking scripts to add your code above it.