| 
29 | 29 |     const sd =  | 
30 | 30 |       script.getAttribute('data-short-domain') ||  | 
31 | 31 |       script.getAttribute('data-domain');  | 
 | 32 | +    const domains = script.getAttribute('data-domains');  | 
32 | 33 | 
 
  | 
33 | 34 |     return {  | 
34 | 35 |       apiHost: ah || 'https://api.dub.co',  | 
35 | 36 |       shortDomain: sd || undefined,  | 
 | 37 | +      domains: domains ? domains.split(',') : undefined,  | 
36 | 38 |       attributionModel: am || 'last-click',  | 
37 | 39 |       cookieOptions: co ? JSON.parse(co) : null,  | 
38 | 40 |       queryParam: qp || 'via',  | 
 | 
101 | 103 |     }  | 
102 | 104 |   }  | 
103 | 105 | 
 
  | 
 | 106 | +  // Support cross-domain tracking  | 
 | 107 | +  function appendCrossDomainClickId(clickId) {  | 
 | 108 | +    const cookie = clickId || getCookie(CLICK_ID);  | 
 | 109 | + | 
 | 110 | +    if (!cookie) {  | 
 | 111 | +      return;  | 
 | 112 | +    }  | 
 | 113 | + | 
 | 114 | +    let { domains } = getOptions(script);  | 
 | 115 | + | 
 | 116 | +    if (!domains || domains.length === 0) {  | 
 | 117 | +      return;  | 
 | 118 | +    }  | 
 | 119 | + | 
 | 120 | +    const currentDomain = window.location.hostname.replace(/^www\./, '');  | 
 | 121 | + | 
 | 122 | +    domains = domains.filter((domain) => domain !== currentDomain);  | 
 | 123 | + | 
 | 124 | +    const selector = domains.map((domain) => `a[href*="${domain}"]`).join(',');  | 
 | 125 | + | 
 | 126 | +    if (!selector || selector.length === 0) {  | 
 | 127 | +      return;  | 
 | 128 | +    }  | 
 | 129 | + | 
 | 130 | +    const links = document.querySelectorAll(selector);  | 
 | 131 | + | 
 | 132 | +    if (!links || links.length === 0) {  | 
 | 133 | +      return;  | 
 | 134 | +    }  | 
 | 135 | + | 
 | 136 | +    links.forEach((link) => {  | 
 | 137 | +      const url = new URL(link.href);  | 
 | 138 | +      url.searchParams.set(CLICK_ID, cookie);  | 
 | 139 | +      link.href = url.toString();  | 
 | 140 | +    });  | 
 | 141 | +  }  | 
 | 142 | + | 
104 | 143 |   // Function to check for { keys } in the URL and update cookie if necessary  | 
105 | 144 |   function watchForQueryParams() {  | 
106 | 145 |     const searchParams = new URLSearchParams(window.location.search);  | 
 | 
111 | 150 | 
 
  | 
112 | 151 |     if (clickId) {  | 
113 | 152 |       checkCookieAndSet(clickId);  | 
 | 153 | +      appendCrossDomainClickId(clickId);  | 
114 | 154 |       return;  | 
115 | 155 |     }  | 
116 | 156 | 
 
  | 
 | 
148 | 188 |       }  | 
149 | 189 |       const { clickId } = await res.json(); // Response: { clickId: string }  | 
150 | 190 |       checkCookieAndSet(clickId);  | 
 | 191 | +      appendCrossDomainClickId(clickId);  | 
151 | 192 |     });  | 
152 | 193 |   }  | 
153 | 194 | 
 
  | 
154 | 195 |   watchForQueryParams();  | 
 | 196 | +  appendCrossDomainClickId();  | 
155 | 197 | 
 
  | 
156 | 198 |   // Listen for URL changes in case of SPA where the page doesn't reload  | 
157 | 199 |   window.addEventListener('popstate', watchForQueryParams);  | 
 | 
0 commit comments