<?php
/**
 * Google Tag Manager renderer.
 *
 * Set $gtm_placement to "head" or "body" before including this file.
 */

$gtmContainerId = function_exists('get_gtm_container_id') ? get_gtm_container_id() : '';
$gtmPlacement = $gtm_placement ?? 'head';
$scriptPath = str_replace('\\', '/', (string)($_SERVER['SCRIPT_NAME'] ?? ''));

if (
    $gtmContainerId !== ''
    && !preg_match('#/(admin|seller|client)/#', $scriptPath)
) {
    if ($gtmPlacement === 'body') {
        ?>
        <!-- Google Tag Manager (noscript) -->
        <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=<?php echo htmlspecialchars($gtmContainerId, ENT_QUOTES, 'UTF-8'); ?>"
        height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
        <!-- End Google Tag Manager (noscript) -->
        <?php
    } else {
        ?>
        <!-- Google Tag Manager -->
        <script>(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','<?php echo htmlspecialchars($gtmContainerId, ENT_QUOTES, 'UTF-8'); ?>');</script>
        <!-- End Google Tag Manager -->
        <?php
    }
}
?>
