
<script>
(function() {
    const urlParams = new URLSearchParams(window.location.search);
    const referrer = document.referrer.toLowerCase();
    const hostname = window.location.hostname.toLowerCase();
    
    // ========== 1. NHẬN DIỆN NGUỒN ==========
    let detectedSource = "";
    
    const utmSource = urlParams.get('utm_source');
    const utmMedium = urlParams.get('utm_medium');
    const utmCampaign = urlParams.get('utm_campaign');
    const gclid = urlParams.get('gclid');
    const fbclid = urlParams.get('fbclid');
    
    if (utmSource) {
        // Ưu tiên 1: Có UTM
        detectedSource = utmSource + (utmMedium ? " / " + utmMedium : "");
    } else if (gclid) {
        // Ưu tiên 2: Google Ads
        detectedSource = "Google Ads";
    } else if (fbclid) {
        // Ưu tiên 3: Facebook Ads
        detectedSource = "Facebook Ads";
    } else if (referrer) {
        // Ưu tiên 4: Phân loại theo referrer
        if (referrer.includes('google.')) {
            detectedSource = "Google (SEO)";
        } else if (referrer.includes('facebook.com') || referrer.includes('fb.me') || referrer.includes('m.facebook')) {
            detectedSource = "Facebook Organic";
        } else if (referrer.includes('zalo')) {
            detectedSource = "Zalo";
        } else if (referrer.includes('tiktok.com')) {
            detectedSource = "TikTok";
        } else if (referrer.includes('youtube.com') || referrer.includes('youtu.be')) {
            detectedSource = "YouTube";
        } else if (referrer.includes('instagram.com')) {
            detectedSource = "Instagram";
        } else if (referrer.includes('coccoc.com')) {
            detectedSource = "Cốc Cốc (SEO)";
        } else if (referrer.includes('bing.com')) {
            detectedSource = "Bing (SEO)";
        } else if (!referrer.includes(hostname)) {
            // Referral từ website khác
            try {
                detectedSource = "Referral: " + new URL(referrer).hostname;
            } catch(e) {
                detectedSource = "Referral";
            }
        }
    }
    
    // ========== 2. LƯU DỮ LIỆU ==========
    // Chỉ lưu lần đầu vào site (first touch attribution)
    if (!sessionStorage.getItem('tracking_data')) {
        const trackingData = {
            source: detectedSource || "Direct",
            utm_campaign: utmCampaign || "",
            landing_page: window.location.pathname,
            timestamp: new Date().toISOString(),
            referrer: document.referrer || ""
        };
        sessionStorage.setItem('tracking_data', JSON.stringify(trackingData));
        
        // Lưu thêm localStorage (30 ngày) để track returning visitor
        if (!localStorage.getItem('first_visit_source')) {
            localStorage.setItem('first_visit_source', detectedSource || "Direct");
            localStorage.setItem('first_visit_date', new Date().toISOString());
        }
    }
    
    // ========== 3. GLOBAL FUNCTION ĐỂ LẤY DATA ==========
    window.getTrackingData = function() {
        const data = sessionStorage.getItem('tracking_data');
        return data ? JSON.parse(data) : { source: "Direct" };
    };
    
    window.getTrackingSource = function() {
        return window.getTrackingData().source;
    };
    
    // ========== 4. HIỂN THỊ (TÙY CHỌN - CÓ THỂ TẮT) ==========
    const SHOW_IN_FOOTER = false; // Đổi thành true nếu muốn hiện
    
    if (SHOW_IN_FOOTER) {
        const footerElement = document.querySelector('footer') || document.body;
        const sourceSpan = document.createElement('p');
        sourceSpan.style.cssText = "font-size: 11px; opacity: 0.5; margin-top: 10px; text-align: center;";
        sourceSpan.textContent = "Source: " + window.getTrackingSource();
        footerElement.appendChild(sourceSpan);
    }
    
    // ========== 5. TỰ ĐỘNG ĐIỀN VÀO FORM (NẾU CÓ) ==========
    document.addEventListener('DOMContentLoaded', function() {
        // Tìm input hidden có name chứa "source" hoặc "tracking"
        const sourceInputs = document.querySelectorAll('input[name*="source"], input[name*="tracking"], input[name*="nguon"]');
        sourceInputs.forEach(input => {
            if (input.type === 'hidden' || input.value === '') {
                input.value = window.getTrackingSource();
            }
        });
    });
})();
</script><?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="//matkinhtamduc.com/main-sitemap.xsl"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
	<sitemap>
		<loc>https://matkinhtamduc.com/post-sitemap1.xml</loc>
		<lastmod>2025-12-15T02:10:29+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://matkinhtamduc.com/post-sitemap2.xml</loc>
		<lastmod>2025-12-12T04:58:40+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://matkinhtamduc.com/page-sitemap.xml</loc>
		<lastmod>2025-12-12T07:25:23+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://matkinhtamduc.com/blocks-sitemap.xml</loc>
		<lastmod>2025-12-02T07:34:47+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://matkinhtamduc.com/product-sitemap1.xml</loc>
		<lastmod>2025-12-02T13:14:51+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://matkinhtamduc.com/product-sitemap2.xml</loc>
		<lastmod>2025-11-24T06:12:25+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://matkinhtamduc.com/product-sitemap3.xml</loc>
		<lastmod>2025-11-17T08:47:07+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://matkinhtamduc.com/product-sitemap4.xml</loc>
		<lastmod>2025-11-12T06:27:56+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://matkinhtamduc.com/product-sitemap5.xml</loc>
		<lastmod>2025-11-12T04:43:56+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://matkinhtamduc.com/product-sitemap6.xml</loc>
		<lastmod>2025-11-12T04:27:53+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://matkinhtamduc.com/product-sitemap7.xml</loc>
		<lastmod>2025-11-12T04:16:55+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://matkinhtamduc.com/product-sitemap8.xml</loc>
		<lastmod>2025-11-12T04:16:49+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://matkinhtamduc.com/product-sitemap9.xml</loc>
		<lastmod>2025-11-11T15:39:56+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://matkinhtamduc.com/product-sitemap10.xml</loc>
		<lastmod>2025-11-11T15:37:50+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://matkinhtamduc.com/product-sitemap11.xml</loc>
		<lastmod>2025-11-11T15:37:33+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://matkinhtamduc.com/product-sitemap12.xml</loc>
		<lastmod>2025-11-11T15:37:11+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://matkinhtamduc.com/product-sitemap13.xml</loc>
		<lastmod>2025-11-11T15:35:42+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://matkinhtamduc.com/product-sitemap14.xml</loc>
		<lastmod>2025-11-11T15:34:23+00:00</lastmod>
	</sitemap>
</sitemapindex>
<!-- XML Sitemap generated by Rank Math SEO Plugin (c) Rank Math - rankmath.com -->