{% if this.shouldRender %}
    {% set gtm_id = this.googleTagManagerId %}
    {% set google_ids = this.googleAnalyticsIds %}
    {% set facebook_ids = this.facebookPixelIds %}
    {% set additional_markup = this.additionalMarkup %}
    {% set fixed_event_markup = this.fixedEventMarkup %}

    {% if this.position == 'head' %}
        {% if gtm_id %}
            <script {% if app.environment == 'prod' %}nonce="{{ csp_nonce('script') }}"{% endif %}>
                (function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
                new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
                j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
                'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
                })(window,document,'script','dataLayer','{{ gtm_id|e('js') }}');
            </script>
        {% endif %}

        {% if google_ids is not empty %}
            <script async src="https://www.googletagmanager.com/gtag/js?id={{ google_ids|first|url_encode }}"></script>
            <script {% if app.environment == 'prod' %}nonce="{{ csp_nonce('script') }}"{% endif %}>
                window.dataLayer = window.dataLayer || [];
                function gtag(){dataLayer.push(arguments);}
                gtag('js', new Date());
                {% for google_id in google_ids %}
                    gtag('config', '{{ google_id|e('js') }}');
                {% endfor %}
            </script>
        {% endif %}

        {% if facebook_ids is not empty %}
            <script {% if app.environment == 'prod' %}nonce="{{ csp_nonce('script') }}"{% endif %}>
                !function(f,b,e,v,n,t,s)
                {if(f.fbq)return;n=f.fbq=function(){n.callMethod?
                n.callMethod.apply(n,arguments):n.queue.push(arguments)};
                if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
                n.queue=[];t=b.createElement(e);t.async=!0;
                t.src=v;s=b.getElementsByTagName(e)[0];
                s.parentNode.insertBefore(t,s)}(window, document,'script',
                'https://connect.facebook.net/en_US/fbevents.js');
                {% for facebook_id in facebook_ids %}
                    fbq('init', '{{ facebook_id|e('js') }}');
                {% endfor %}
                fbq('track', 'PageView');
            </script>
        {% endif %}

        {% if additional_markup %}
            {{ additional_markup|raw }}
        {% endif %}
    {% else %}
        {% if gtm_id %}
            <noscript>
                <iframe
                    src="https://www.googletagmanager.com/ns.html?id={{ gtm_id|url_encode }}"
                    height="0"
                    width="0"
                    style="display:none;visibility:hidden"
                ></iframe>
            </noscript>
        {% endif %}

        {% if facebook_ids is not empty %}
            {% for facebook_id in facebook_ids %}
                <noscript>
                    <img
                        height="1"
                        width="1"
                        style="display:none"
                        alt=""
                        src="https://www.facebook.com/tr?id={{ facebook_id|url_encode }}&ev=PageView&noscript=1"
                    />
                </noscript>
            {% endfor %}
        {% endif %}

        {% if additional_markup %}
            {{ additional_markup|raw }}
        {% endif %}

        {% if fixed_event_markup %}
            {{ fixed_event_markup|raw }}
        {% endif %}
    {% endif %}
{% endif %}
