PageSourceSearch

https://builtbyveterans.ai/assets/js/attribution.js

js builtbyveterans.ai collected 2026-10-05 11:53:03 UTC 2,678 bytes, 82 lines download raw bytes

1/**
2 * Built By Veterans — Marketing Attribution Capture
3 * Captures UTM parameters, click IDs, and GA4 Client ID on first page visit
4 * Stores in sessionStorage for later use in conversion tracking
5 * v1.0
6 */
7(function() {
8  'use strict';
9  
10  // Get URL parameter by name
11  function getUrlParam(name) {
12    const urlParams = new URLSearchParams(window.location.search);
13    return urlParams.get(name);
14  }
15  
16  // Get GA4 Client ID from cookie or gtag API
17  function getGAClientID() {
18    // Try to extract from GA4 cookie (_ga_XXXXXXXXXX)
19    const cookies = document.cookie.split(';');
20    for (let i = 0; i < cookies.length; i++) {
21      const cookie = cookies[i].trim();
22      if (cookie.startsWith('_ga_')) {
23        // Cookie format: _ga_XXXXXXXXXX=GS1.1.timestamp.random.random
24        const match = cookie.match(/=GS1\.1\.(\d+\.\d+\.\d+\.\d+)/);
25        if (match) return match[1];
26      }
27    }
28    
29    // Try to get from gtag API if available
30    if (typeof gtag === 'function') {
31      return new Promise(function(resolve) {
32        gtag('get', 'G-KVR8G1CEJL', 'client_id', function(clientId) {
33          resolve(clientId || null);
34        });
35      });
36    }
37    
38    return null;
39  }
40  
41  // Capture and store attribution data
42  function captureAttribution() {
43    // Only capture once per session
44    if (sessionStorage.getItem('bbv_attribution_captured')) {
45      return;
46    }
47    
48    // Collect all attribution parameters
49    const attribution = {
50      utm_source: getUrlParam('utm_source'),
51      utm_medium: getUrlParam('utm_medium'),
52      utm_campaign: getUrlParam('utm_campaign'),
53      utm_term: getUrlParam('utm_term'),
54      utm_content: getUrlParam('utm_content'),
55      gclid: getUrlParam('gclid'),        // Google Ads click ID
56      fbclid: getUrlParam('fbclid'),      // Facebook Ads click ID
57      referrer: document.referrer || null,
58      landing_page: window.location.href,
59      timestamp: new Date().toISOString()
60    };
61    
62    // Get GA Client ID (may be async)
63    Promise.resolve(getGAClientID()).then(function(clientId) {
64      if (clientId) {
65        attribution.ga_client_id = clientId;
66      }
67      
68      // Store attribution data
69      sessionStorage.setItem('bbv_attribution', JSON.stringify(attribution));
70      sessionStorage.setItem('bbv_attribution_captured', 'true');
71      
72      console.log('✅ Attribution captured:', attribution);
73    });
74  }
75  
76  // Run on page load
77  if (document.readyState === 'loading') {
78    document.addEventListener('DOMContentLoaded', captureAttribution);
79  } else {
80    captureAttribution();
81  }
82})();

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.