PageSourceSearch

https://members-cdn.schiffsovereign.com/wp-content/themes/sovereig…/js/sm-tracking.js?ver=1779199841

js schiffsovereign.com collected 2026-10-02 03:40:13 UTC 14,115 bytes, 564 lines download raw bytes

1(function () {
2  var debugEnabled = false;
3  try {
4    debugEnabled = window.localStorage && window.localStorage.getItem('sm_tracking_debug') === '1';
5  } catch (e) {
6    debugEnabled = false;
7  }
8
9  var traceEnabled = false;
10  try {
11    traceEnabled = window.localStorage && window.localStorage.getItem('sm_tracking_trace') === '1';
12  } catch (e) {
13    traceEnabled = false;
14  }
15
16  function trace() {
17    if (!traceEnabled) {
18      return;
19    }
20    try {
21      if (typeof console !== 'undefined' && typeof console.log === 'function') {
22        var args = Array.prototype.slice.call(arguments);
23        args.unshift('[smTracking]');
24        //console.log.apply(console, args);
25      }
26    } catch (e) {
27    }
28  }
29
30  var queueStorageKey = 'sm_tracking_queue_v1';
31  var queueMaxLength = 50;
32  var queueCleanupDelayMs = 5000;
33  var eventIdCounter = 0;
34  var memoryQueue = [];
35
36  function getNowTs() {
37    return Date.now ? Date.now() : new Date().getTime();
38  }
39
40  function makeEventId() {
41    eventIdCounter = (eventIdCounter + 1) % 1000000;
42    return 'sm_' + getNowTs().toString(36) + '_' + eventIdCounter.toString(36);
43  }
44
45  function getSessionStorageSafe() {
46    try {
47      if (!window.sessionStorage) {
48        trace('sessionStorage unavailable');
49        return null;
50      }
51      var k = '__sm_tracking_test__';
52      window.sessionStorage.setItem(k, '1');
53      window.sessionStorage.removeItem(k);
54      return window.sessionStorage;
55    } catch (e) {
56      trace('sessionStorage blocked', e);
57      return null;
58    }
59  }
60
61  function readQueue() {
62    var storage = getSessionStorageSafe();
63    if (!storage) {
64      return memoryQueue.slice();
65    }
66    try {
67      var raw = storage.getItem(queueStorageKey);
68      if (!raw) {
69        return [];
70      }
71      var parsed = JSON.parse(raw);
72      return Array.isArray(parsed) ? parsed : [];
73    } catch (e) {
74      return [];
75    }
76  }
77
78  function writeQueue(queue) {
79    var storage = getSessionStorageSafe();
80    if (!storage) {
81      memoryQueue = queue || [];
82      return;
83    }
84    try {
85      storage.setItem(queueStorageKey, JSON.stringify(queue || []));
86    } catch (e) {
87    }
88  }
89
90  function removeFromQueueById(id) {
91    if (!id) {
92      return;
93    }
94    var queue = readQueue();
95    if (!queue.length) {
96      return;
97    }
98    var next = [];
99    for (var i = 0; i < queue.length; i++) {
100      if (queue[i] && queue[i].id !== id) {
101        next.push(queue[i]);
102      }
103    }
104    writeQueue(next);
105    trace('queue remove', id, 'remaining', next.length);
106  }
107
108  function enqueueEventRecord(record) {
109    if (!record) {
110      return;
111    }
112    var queue = readQueue();
113    queue.push(record);
114    if (queue.length > queueMaxLength) {
115      queue = queue.slice(queue.length - queueMaxLength);
116    }
117    writeQueue(queue);
118    trace('queue enqueue', record.name, record.id, 'size', queue.length);
119  }
120
121  function flushQueue() {
122    var gtag = ensureGtag();
123    if (!gtag) {
124      trace('flush skipped: gtag not ready');
125      return;
126    }
127
128    var queue = readQueue();
129    if (!queue.length) {
130      trace('flush: queue empty');
131      return;
132    }
133
134    trace('flush start', 'count', queue.length);
135
136    var now = getNowTs();
137    var keep = [];
138    for (var i = 0; i < queue.length; i++) {
139      var item = queue[i];
140      if (!item || !item.name) {
141        continue;
142      }
143      if (item.ts && now - item.ts > 60000) {
144        continue;
145      }
146      var payload = item.params || {};
147
148      if (payload && typeof payload.event_callback === 'function') {
149        trace('flush sanitize: removing event_callback', item.name, item.id);
150        try {
151          delete payload.event_callback;
152        } catch (e) {
153        }
154      }
155
156      if (debugEnabled) {
157        payload.debug_mode = true;
158      }
159      if (typeof payload.transport_type === 'undefined') {
160        payload.transport_type = 'beacon';
161      }
162      if (!payload.event_id && item.id) {
163        payload.event_id = item.id;
164      }
165      try {
166        gtag('event', item.name, payload);
167        trace('flush sent', item.name, payload.event_id || item.id);
168      } catch (e) {
169        trace('flush send error', item.name, e);
170        keep.push(item);
171      }
172    }
173
174    writeQueue(keep);
175    trace('flush done', 'remaining', keep.length);
176  }
177
178  function scheduleFlushRetries() {
179    var attempts = 0;
180    var maxAttempts = 10;
181    var intervalMs = 500;
182    var intervalId = window.setInterval(function () {
183      attempts++;
184      try {
185        trace('flush retry', attempts);
186        flushQueue();
187      } catch (e) {
188      }
189      if (attempts >= maxAttempts) {
190        window.clearInterval(intervalId);
191        return;
192      }
193      try {
194        if (!readQueue().length) {
195          window.clearInterval(intervalId);
196        }
197      } catch (e) {
198      }
199    }, intervalMs);
200  }
201
202  function shouldDelayNavigationForAnchor(a, isModifiedClick) {
203    if (!a || isModifiedClick) {
204      return false;
205    }
206    var target = safeTrim(a.getAttribute('target'));
207    if (target && target !== '_self') {
208      return false;
209    }
210    var hrefAttr = safeTrim(a.getAttribute('href'));
211    if (!hrefAttr) {
212      return false;
213    }
214    var hrefLower = hrefAttr.toLowerCase();
215    if (hrefLower.indexOf('javascript:') === 0) {
216      return false;
217    }
218    if (hrefAttr.charAt(0) === '#') {
219      return false;
220    }
221    return true;
222  }
223
224  function attachNavigationCallback(e, href, payload) {
225    if (!e || !href || !payload) {
226      return;
227    }
228    e.preventDefault();
229    var didNavigate = false;
230    var navigate = function () {
231      if (didNavigate) {
232        return;
233      }
234      didNavigate = true;
235      window.location.href = href;
236    };
237    payload.event_callback = navigate;
238    payload.event_timeout = 800;
239    window.setTimeout(navigate, 900);
240  }
241
242  function ensureGtag() {
243    if (typeof window.gtag === 'function') {
244      return window.gtag;
245    }
246    if (Array.isArray(window.dataLayer)) {
247      window.gtag = function () {
248        window.dataLayer.push(arguments);
249      };
250      return window.gtag;
251    }
252    return null;
253  }
254
255  function safeTrim(s) {
256    return (s || '').toString().replace(/\s+/g, ' ').trim();
257  }
258
259  function getAnchorEl(el) {
260    if (!el) {
261      return null;
262    }
263    if (el.tagName && el.tagName.toLowerCase() === 'a') {
264      return el;
265    }
266    if (typeof el.closest === 'function') {
267      return el.closest('a');
268    }
269    return null;
270  }
271
272  function isProbablyDownload(pathname) {
273    var p = safeTrim(pathname).toLowerCase();
274    return (
275      p.endsWith('.pdf') ||
276      p.endsWith('.zip') ||
277      p.endsWith('.doc') ||
278      p.endsWith('.docx') ||
279      p.endsWith('.xls') ||
280      p.endsWith('.xlsx') ||
281      p.endsWith('.ppt') ||
282      p.endsWith('.pptx')
283    );
284  }
285
286  function getHost(url) {
287    try {
288      return new URL(url, window.location.href).host;
289    } catch (e) {
290      return '';
291    }
292  }
293
294  function getPathname(url) {
295    try {
296      return new URL(url, window.location.href).pathname;
297    } catch (e) {
298      return '';
299    }
300  }
301
302  function track(eventName, params) {
303    var payload = params || {};
304    if (debugEnabled) {
305      payload.debug_mode = true;
306    }
307    if (typeof payload.transport_type === 'undefined') {
308      payload.transport_type = 'beacon';
309    }
310
311    if (!payload.event_id) {
312      payload.event_id = makeEventId();
313    }
314
315    var queuedParams = {};
316    try {
317      for (var k in payload) {
318        if (!Object.prototype.hasOwnProperty.call(payload, k)) {
319          continue;
320        }
321        if (typeof payload[k] === 'function') {
322          continue;
323        }
324        queuedParams[k] = payload[k];
325      }
326    } catch (e) {
327      queuedParams = payload;
328    }
329
330    enqueueEventRecord({
331      id: payload.event_id,
332      name: eventName,
333      params: queuedParams,
334      ts: getNowTs(),
335    });
336
337    window.setTimeout(function () {
338      removeFromQueueById(payload.event_id);
339    }, queueCleanupDelayMs);
340
341    var gtag = ensureGtag();
342    if (!gtag) {
343      trace('send skipped: gtag not ready (queued)', eventName, payload.event_id);
344      return true;
345    }
346
347    try {
348      if (debugEnabled && typeof console !== 'undefined' && typeof console.log === 'function') {
349        //console.log('[smTrack]', eventName, payload);
350      }
351      trace('send', eventName, payload.event_id);
352      gtag('event', eventName, payload);
353      return true;
354    } catch (e) {
355      trace('send error', eventName, e);
356      return false;
357    }
358  }
359
360  window.smTrack = function (eventName, params) {
361    return track(eventName, params);
362  };
363
364  document.addEventListener(
365    'click',
366    function (e) {
367      var el = e.target;
368      if (!el || typeof el.closest !== 'function') {
369        return;
370      }
371
372      var isModifiedClick =
373        e.defaultPrevented ||
374        e.button === 1 ||
375        e.metaKey ||
376        e.ctrlKey ||
377        e.shiftKey ||
378        e.altKey;
379
380      var trackedEl = el.closest('[data-sm-track]');
381      if (!trackedEl) {
382        var aFallback = getAnchorEl(el);
383        if (!aFallback) {
384          return;
385        }
386
387        var hrefFallback = aFallback.href;
388        if (!hrefFallback) {
389          return;
390        }
391
392        var linkTextFallback = safeTrim(aFallback.textContent);
393        var host = getHost(hrefFallback);
394        var pathname = getPathname(hrefFallback);
395        var isOutbound = host && host !== window.location.host;
396        var isDownload = isProbablyDownload(pathname);
397
398        var isMegaMenuLink = false;
399        if (typeof aFallback.closest === 'function') {
400          isMegaMenuLink = !!aFallback.closest('#mega-menu-header_menu');
401        }
402
403        if (isMegaMenuLink) {
404          var label = linkTextFallback || pathname || hrefFallback;
405          var payload = {
406            event_category: 'navigation',
407            event_label: label,
408            sm_location: 'header_menu',
409            link_url: hrefFallback,
410            link_path: pathname,
411            link_text: linkTextFallback,
412          };
413
414          if (shouldDelayNavigationForAnchor(aFallback, isModifiedClick)) {
415            attachNavigationCallback(e, hrefFallback, payload);
416          }
417
418          track('menu_click', payload);
419          return;
420        }
421
422        if (!isOutbound && !isDownload) {
423          return;
424        }
425
426        if (isDownload) {
427          var downloadPayload = {
428            event_category: 'download',
429            event_label: pathname || hrefFallback,
430            sm_location: 'auto',
431            link_url: hrefFallback,
432            link_path: pathname,
433            link_text: linkTextFallback,
434          };
435          if (shouldDelayNavigationForAnchor(aFallback, isModifiedClick)) {
436            attachNavigationCallback(e, hrefFallback, downloadPayload);
437          }
438          track('download_click', downloadPayload);
439          return;
440        }
441
442        var outboundPayload = {
443          event_category: 'outbound',
444          event_label: host || hrefFallback,
445          sm_location: 'auto',
446          link_url: hrefFallback,
447          link_path: pathname,
448          link_text: linkTextFallback,
449        };
450        if (shouldDelayNavigationForAnchor(aFallback, isModifiedClick)) {
451          attachNavigationCallback(e, hrefFallback, outboundPayload);
452        }
453        track('outbound_click', outboundPayload);
454        return;
455      }
456
457      var eventName = safeTrim(trackedEl.getAttribute('data-sm-track')) || 'sm_click';
458      var category = safeTrim(trackedEl.getAttribute('data-sm-track-category')) || 'link';
459      var label = safeTrim(trackedEl.getAttribute('data-sm-track-label'));
460      var location = safeTrim(trackedEl.getAttribute('data-sm-track-location'));
461
462      var a = getAnchorEl(trackedEl);
463      var href = a && a.href ? a.href : safeTrim(trackedEl.getAttribute('href'));
464      var linkText = a ? safeTrim(a.textContent) : safeTrim(trackedEl.textContent);
465
466      var shouldDelayNavigation = false;
467      var navHref = '';
468      if (a && href && shouldDelayNavigationForAnchor(a, isModifiedClick)) {
469        shouldDelayNavigation = true;
470        navHref = href;
471      }
472
473      if (!label) {
474        label = linkText || href || 'unknown';
475      }
476
477      var linkPath = '';
478      try {
479        if (href) {
480          linkPath = new URL(href, window.location.href).pathname;
481        }
482      } catch (err) {
483        linkPath = '';
484      }
485
486      var payload = {
487        event_category: category,
488        event_label: label,
489        sm_location: location,
490        link_url: href,
491        link_path: linkPath,
492        link_text: linkText,
493      };
494
495      var card = null;
496      if (typeof trackedEl.closest === 'function') {
497        card = trackedEl.closest('article.content-card');
498      }
499      if (card) {
500        var cardProduct = safeTrim(card.getAttribute('data-product'));
501        var cardProductId = safeTrim(card.getAttribute('data-product-id'));
502        var cardWordCount = safeTrim(card.getAttribute('data-word-count'));
503        var cardPdfUrl = safeTrim(card.getAttribute('data-pdf-url'));
504
505        if (cardProduct) {
506          payload.content_product = cardProduct;
507        }
508        if (cardProductId) {
509          payload.content_product_id = cardProductId;
510        }
511        if (cardWordCount) {
512          payload.content_word_count = parseInt(cardWordCount, 10);
513        }
514        if (cardPdfUrl) {
515          payload.content_pdf_url = cardPdfUrl;
516        }
517        if (label) {
518          payload.content_post_id = label;
519        }
520      }
521
522      if (shouldDelayNavigation) {
523        attachNavigationCallback(e, navHref, payload);
524      }
525
526      track(eventName, payload);
527    },
528    { capture: true }
529  );
530
531  try {
532    flushQueue();
533  } catch (e) {
534  }
535
536  try {
537    scheduleFlushRetries();
538  } catch (e) {
539  }
540
541  window.addEventListener(
542    'pagehide',
543    function () {
544      try {
545        flushQueue();
546      } catch (e) {
547      }
548    },
549    { capture: true }
550  );
551
552  document.addEventListener(
553    'visibilitychange',
554    function () {
555      if (document.visibilityState === 'hidden') {
556        try {
557          flushQueue();
558        } catch (e) {
559        }
560      }
561    },
562    { capture: true }
563  );
564})();

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.