/* Theme Name: Autoglow Child Theme
Theme URI: https://themeforest.net/user/ovatheme/portfolio
Description: This is child theme of autoglow
Author: Ovatheme
Author URI: https://themeforest.net/user/ovatheme
Template: autoglow
Version: 1.0.0
Tags: two-columns, three-columns, left-sidebar, right-sidebar, custom-menu, editor-style, featured-images, flexible-header, full-width-template, post-formats, rtl-language-support, sticky-post, translation-ready
Text Domain:  autoglow-child
*/




add_action('wp_footer', function () { ?>
  <!-- Analytics Tracking Code -->
  <script>
  (function(){
    const endpoint = "https://analytics.elektronikyazilim.com.tr/t.php";
    const site = "ELI_0e9f742da69211f093b7";
    const beatMs = 12000;
    const isMob  = /Mobi|Android|iPhone|iPad|iPod/i.test(navigator.userAgent);

    const COOKIE_NAME = "eli_client_id_" + site.replace(/[^A-Za-z0-9_]/g, "_");

    function getCookie(name) {
      const parts = document.cookie ? document.cookie.split("; ") : [];
      for (const p of parts) {
        const idx = p.indexOf("=");
        if (idx === -1) continue;

        const k = decodeURIComponent(p.slice(0, idx));
        if (k === name) {
          return decodeURIComponent(p.slice(idx + 1));
        }
      }

      return "";
    }

    function setCookie(name, value, days) {
      const maxAge = days * 86400;
      const secure = location.protocol === "https:" ? "; Secure" : "";

      document.cookie =
        encodeURIComponent(name) + "=" + encodeURIComponent(value) +
        "; Path=/; Max-Age=" + maxAge + "; SameSite=Lax" + secure;
    }

    function newId(prefix) {
      let rnd = "";

      try {
        rnd = crypto && crypto.randomUUID
          ? crypto.randomUUID()
          : Math.random().toString(36).slice(2);
      } catch(e) {
        rnd = Math.random().toString(36).slice(2);
      }

      return prefix + "-" + rnd + "-" + Date.now();
    }

    let cid = getCookie(COOKIE_NAME);

    if (!cid || !/^CID-[a-zA-Z0-9._-]{8,120}$/.test(cid)) {
      cid = newId("CID");
      setCookie(COOKIE_NAME, cid, 365);
    }

    let tab = "main";

    let timer = null;
    let byeSent = false;

    const makeBody = (extra) => JSON.stringify({
      site_key: site,
      client_id: cid,
      tab: tab,
      path: location.pathname + location.search + location.hash,
      title: document.title,
      referrer: document.referrer || null,
      is_mobile: isMob,
      ...(extra || {})
    });

    function send(body){
      if (navigator.sendBeacon) {
        try {
          const ok = navigator.sendBeacon(
            endpoint,
            new Blob([body], {type: "text/plain"})
          );

          if (ok) return;
        } catch(e){}
      }

      try {
        fetch(endpoint, {
          method: "POST",
          keepalive: true,
          headers: {"Content-Type": "text/plain"},
          body: body
        }).catch(function(){});
      } catch(e){}
    }

    function beat(){
      if (!byeSent) {
        send(makeBody());
      }
    }

    function bye(){
      if (byeSent) return;

      byeSent = true;

      if (timer) {
        clearInterval(timer);
        timer = null;
      }

      send(makeBody({bye: true}));
    }

    // İlk kayıt
    beat();

    // Periyodik heartbeat
    timer = setInterval(beat, beatMs);

    // Sayfa tekrar görünür olunca
    document.addEventListener("visibilitychange", function(){
      if (document.visibilityState === "visible") {
        beat();
      }
    }, {passive: true});

    // Kapanış
    window.addEventListener("pagehide", bye, {passive: true});
    window.addEventListener("beforeunload", bye, {passive: true});

    // BFCache dönüşü
    window.addEventListener("pageshow", function(e){
      if (e.persisted) {
        byeSent = false;
        beat();

        if (!timer) {
          timer = setInterval(beat, beatMs);
        }
      }
    }, {passive: true});

    // SPA URL değişimi
    (function hookHistory(){
      let lastHref = location.href;

      function onUrlChange(){
        const current = location.href;

        if (current === lastHref) return;

        lastHref = current;
        beat();
      }

      const _ps = history.pushState;
      const _rs = history.replaceState;

      history.pushState = function(){
        const r = _ps.apply(this, arguments);
        onUrlChange();
        return r;
      };

      history.replaceState = function(){
        const r = _rs.apply(this, arguments);
        onUrlChange();
        return r;
      };

      window.addEventListener("popstate", onUrlChange, {passive: true});
      window.addEventListener("hashchange", onUrlChange, {passive: true});
    })();
  })();
  </script>

  <!-- Pixel: bot/no-JS istekleri ayrıca yakalamak için -->
  <img src="https://analytics.elektronikyazilim.com.tr/t_pixel.php?site=ELI_0e9f742da69211f093b7&p=/" alt="" width="1" height="1" style="display:none" loading="eager" decoding="async">
<?php }, 999);


add_action('init', function () {
    $tracker = $_SERVER['DOCUMENT_ROOT'] . '/eli_server_tracker.php';

    if (is_file($tracker)) {
        require_once $tracker;
    }
}, 1);
