Pixel Setup

Configure Your Pixel
Customize the tracking pixel to fit your website's needs.
Your Tracking Pixel
Copy this snippet and paste it into the <head> section of your website.

<script>
// amitami Attribution Pixel
// API Key: ts_key_1a2b3c4d5e6f7g8h9i0j
window.addEventListener('load', () => {
  setTimeout(() => {
    if (window.__visitTracked) return;
    window.__visitTracked = true;

    // IMPORTANT: Replace with your actual API endpoint
    const API_ENDPOINT = 'https://your-website.com/api/track'; 
    const API_KEY = 'ts_key_1a2b3c4d5e6f7g8h9i0j';
    const DEBUG = new URLSearchParams(window.location.search).has('debug-touch');
    const SESSION_TIMEOUT_MINUTES = 30;

    const hasConverted = localStorage.getItem('ts_has_converted') === 'true';

    if (hasConverted && !DEBUG) return;

    function generateUUID() {
      return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
        (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
      );
    }

    function getUTMParams() {
        const params = new URLSearchParams(window.location.search);
        const utmKeys = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term'];
        let utms = {};
        let found = false;

        utmKeys.forEach(key => {
            if (params.has(key)) {
                localStorage.setItem('ts_' + key, params.get(key));
                found = true;
            }
        });

        if (!found && document.referrer) {
            const referrerHost = new URL(document.referrer).hostname;
            if (!referrerHost.includes(window.location.hostname)) {
                 localStorage.setItem('ts_utm_source', referrerHost);
            }
        } else if (!found) {
            localStorage.setItem('ts_utm_source', 'direct');
        }

        utmKeys.forEach(key => {
            utms[key] = localStorage.getItem('ts_' + key) || '';
        });
        
        return utms;
    }

    function isNewSession() {
      const lastTime = localStorage.getItem('ts_last_touch_time');
      if (!lastTime) return true;
      const elapsedMinutes = (Date.now() - parseInt(lastTime)) / (1000 * 60);
      return elapsedMinutes > SESSION_TIMEOUT_MINUTES;
    }

    let userUUID = localStorage.getItem('ts_user_uuid');
    if (!userUUID) {
      userUUID = generateUUID();
      localStorage.setItem('ts_user_uuid', userUUID);
    }

    let touchNumber = parseInt(localStorage.getItem('ts_touch_number') || '0');
    
    // Track page visit
    const now = Date.now();
    let sessionId = localStorage.getItem('ts_session_id');

    if (isNewSession()) {
        sessionId = generateUUID();
        localStorage.setItem('ts_session_id', sessionId);
        touchNumber += 1;
        localStorage.setItem('ts_touch_number', touchNumber);
    }
    localStorage.setItem('ts_last_touch_time', now.toString());
    
    const utms = getUTMParams();
    const payload = {
      apiKey: API_KEY,
      data: {
        uuid: userUUID,
        touch_number: touchNumber,
        session_id: sessionId,
        event_type: 'visit',
        page_url: window.location.href,
        page_title: document.title,
        referrer: document.referrer,
        ...utms
      }
    };
    
    if (DEBUG) console.log('[amitami] Tracking visit:', payload);
    navigator.sendBeacon(API_ENDPOINT, JSON.stringify(payload));
    
    // Function to track form submissions
    function trackFormSubmission(form) {
      if (localStorage.getItem('ts_has_converted') === 'true' && !DEBUG) return;
      localStorage.setItem('ts_has_converted', 'true');
      
      const formData = new FormData(form);
      const formEntries = Object.fromEntries(formData.entries());

      const submissionPayload = {
        apiKey: API_KEY,
        data: {
          uuid: userUUID,
          touch_number: touchNumber + 1, // Increment touch for conversion event
          session_id: sessionId,
          event_type: 'conversion',
          page_url: window.location.href,
          page_title: document.title,
          form_data: formEntries,
          ...utms
        }
      };

      if (DEBUG) console.log('[amitami] Tracking conversion:', submissionPayload);
      navigator.sendBeacon(API_ENDPOINT, JSON.stringify(submissionPayload));
    }
    
    // Example for Contact Form 7
    document.addEventListener('wpcf7mailsent', (event) => trackFormSubmission(event.target), false);
    
    // Add listeners for other forms if needed
    // document.querySelectorAll('form').forEach(form => {
    //   form.addEventListener('submit', () => trackFormSubmission(form));
    // });

  }, 300);
});
</script>

Security Enhancement:

We've updated your pixel to send data to a secure API endpoint on your server instead of directly to a third-party service. This keeps your API keys and data handling private. You'll need to create this endpoint (`/api/track`) to receive the data.