PageSourceSearch

https://themeatclub.dk/static/version1788870852/frontend/themeatcl…/js/profitmetrics/pmTPTrack.js?v=1.1.46

js themeatclub.dk collected 2026-10-08 17:09:06 UTC 17,058 bytes, 375 lines download raw bytes

1require([
2        'jquery',
3        'mage/cookies',
4        'Magento_Customer/js/customer-data'
5    ],
6    function (
7        $,
8        mageCookies,
9        customerData
10    ) {
11        (function() {
12            'use strict';
13
14            const COOKIE_NAME = 'pmTPTrack';
15            const CONSENT_STORAGE_KEY = 'pfm-consent-granted';
16            const DEFAULT_TIMESTAMP_OFFSET = 100;
17
18            function getQueryParam(paramName) {
19                const regex = new RegExp("[\\?&]" + paramName + "=([^&#]*)");
20                const result = regex.exec(location.search);
21                return result === null ? "" : decodeURIComponent(result[1].replace(/\+/g, " "));
22            }
23
24            function parseGclidCookie(cookieValue) {
25                if (!cookieValue) {
26                    return null;
27                }
28
29                const parts = cookieValue.split('.');
30                if (parts.length >= 3) {
31                    const gclid = parts.slice(2).join('.');
32                    return gclid ? gclid : null;
33                }
34
35                return null;
36            }
37
38            function getGoogleClickId(allCookies) {
39                const urlGclid = getQueryParam("gclid");
40                if (urlGclid) {
41                    return urlGclid;
42                }
43
44                const gclAwGclid = parseGclidCookie(allCookies['_gcl_aw']);
45                if (gclAwGclid) {
46                    return gclAwGclid;
47                }
48
49                const fpgclawGclid = parseGclidCookie(allCookies['FPGCLAW']);
50                if (fpgclawGclid) {
51                    return fpgclawGclid;
52                }
53
54                return "";
55            }
56
57            function parseGACookies() {
58                const gaCookieRegex = /^_ga_([A-Z0-9]+)/;
59                return document.cookie.split(';').map(cookie => {
60                    const trimmedCookie = cookie.trim();
61                    const match = trimmedCookie.match(gaCookieRegex);
62                    if (!match) {
63                        return null;
64                    }
65
66                    const measurementId = match[1];
67                    const cookieValue = trimmedCookie.substring(trimmedCookie.indexOf('=') + 1);
68                    const cookieParts = cookieValue.split('.');
69                    const isGS2Format = cookieValue.includes('GS2');
70
71                    const parsedData = { measurementId, sessionId: null, sessionCount: null };
72
73                    if (isGS2Format && cookieParts.length >= 3) {
74                        const dataSegments = cookieParts[2].split('$');
75                        const sessionSegment = dataSegments.find(segment => segment.startsWith('s'));
76                        const countSegment = dataSegments.find(segment => segment.startsWith('o'));
77                        if (sessionSegment) {
78                            parsedData.sessionId = sessionSegment.substring(1);
79                        }
80                        if (countSegment) {
81                            parsedData.sessionCount = countSegment.substring(1);
82                        }
83                    } else if (!isGS2Format) {
84                        if (cookieParts.length >= 3) {
85                            parsedData.sessionId = cookieParts[2];
86                        }
87                        if (cookieParts.length >= 5) {
88                            parsedData.sessionCount = cookieParts[4];
89                        }
90                    }
91                    return parsedData;
92                }).filter(Boolean);
93            }
94
95            function loadTrackingData() {
96                // Use jQuery cookies method instead of mageCookies
97                const stored = $.cookie(COOKIE_NAME);
98                if (stored) {
99                    try {
100                        // Try parsing directly first (jQuery cookie may handle encoding)
101                        return JSON.parse(stored);
102                    } catch (e) {
103                        try {
104                            return JSON.parse(decodeURIComponent(stored));
105                        } catch (e2) {
106                        }
107                    }
108                }
109                return {
110                    gclid: null, gacid: null, gacid_source: null, fbp: null, fbc: null,
111                    gbraid: null, wbraid: null, sccid: null, ttclid: null, msclkid: null,
112                    twclid: null, ga4SessionId: null, ga4SessionCount: null,
113                    timestamp: (Math.floor(Date.now() / 1000) - DEFAULT_TIMESTAMP_OFFSET)
114                };
115            }
116
117            function saveTrackingData(trackingData) {
118                const cookieValue = JSON.stringify(trackingData);
119
120                // Use jQuery cookies method instead of mageCookies
121                $.cookie(COOKIE_NAME, cookieValue, {
122                    path: '/',
123                    expires: 90 // 90 days
124                });
125
126            }
127
128            function mergeTrackingUpdates(trackingData, updates) {
129                let hasChanges = false;
130                const newData = { ...trackingData };
131
132                for (const [key, value] of Object.entries(updates)) {
133                    if (value !== null && value !== undefined && value !== "" && newData[key] !== value) {
134                        if (key === 'gacid' && newData.gacid_source === 'gatracker' && updates.gacid_source !== 'gatracker') {
135                            continue;
136                        }
137                        newData[key] = value;
138                        hasChanges = true;
139                    }
140                }
141
142                if (hasChanges) {
143                    newData.timestamp = Math.floor(Date.now() / 1000);
144                }
145                return newData;
146            }
147
148            function fetchAnalyticsClientId(currentData, retries = 10) {
149                if (typeof ga !== 'function') {
150                    if (retries > 0) {
151                        setTimeout(() => fetchAnalyticsClientId(currentData, retries - 1), 100);
152                    }
153                    return;
154                }
155                try {
156                    ga(function(tracker) {
157                        const gacid = tracker.get('clientId');
158                        if (gacid && (currentData.gacid !== gacid || currentData.gacid_source !== 'gatracker')) {
159                            const updatedData = mergeTrackingUpdates(currentData, {
160                                gacid: gacid,
161                                gacid_source: 'gatracker'
162                            });
163                            saveTrackingData(updatedData);
164                        }
165                    });
166                } catch (error) {
167                    // Silent
168                }
169            }
170
171            function init() {
172
173                let trackingData;
174                try {
175                    trackingData = loadTrackingData();
176                } catch (error) {
177                    return;
178                }
179
180                const allCookies = document.cookie.split(';').reduce((acc, cookie) => {
181                    const parts = cookie.trim().split('=');
182                    acc[parts.shift()] = parts.join('=');
183                    return acc;
184                }, {});
185
186                const gaCookieData = parseGACookies();
187                const { sessionIds, sessionCounts } = gaCookieData.reduce((acc, data) => {
188                    if (data.sessionId) {
189                        acc.sessionIds.push(`${data.measurementId}:${data.sessionId}`);
190                    }
191                    if (data.sessionCount) {
192                        acc.sessionCounts.push(`${data.measurementId}:${data.sessionCount}`);
193                    }
194                    return acc;
195                }, { sessionIds: [], sessionCounts: [] });
196
197                const updates = {
198                    fbc: allCookies["_fbc"] || null,
199                    fbp: allCookies["_fbp"] || null,
200                    gacid: allCookies["_ga"] || null,
201                    gacid_source: null,
202                    gclid: getGoogleClickId(allCookies),
203                    gbraid: getQueryParam("gbraid"),
204                    wbraid: getQueryParam("wbraid"),
205                    sccid: getQueryParam("sccid"),
206                    ttclid: getQueryParam("ttclid"),
207                    msclkid: getQueryParam("msclkid"),
208                    twclid: getQueryParam("twclid"),
209                    ga4SessionId: sessionIds.join(','),
210                    ga4SessionCount: sessionCounts.join(','),
211                    cc_marketing: window.cc_marketing,
212                    cc_statistics: window.cc_statistics
213                };
214
215                trackingData = mergeTrackingUpdates(trackingData, updates);
216                saveTrackingData(trackingData);
217
218                document.dispatchEvent(new CustomEvent('profitmetrics:consentUpdated', {
219                    detail: { marketing: window.cc_marketing, statistics: window.cc_statistics }
220                }));
221
222                fetchAnalyticsClientId(trackingData);
223
224                const consentGranted = window.cc_marketing || window.cc_statistics;
225                localStorage.setItem(CONSENT_STORAGE_KEY, consentGranted);
226            }
227
228            window.cc_statistics = false;
229            window.cc_marketing = false;
230
231            function checkCookiebotConsent() {
232                if (typeof Cookiebot !== 'undefined' && Cookiebot.consent) {
233                    return { statistics: Cookiebot.consent.statistics, marketing: Cookiebot.consent.marketing };
234                }
235                return null;
236            }
237
238            function checkCookieInformationConsent() {
239                if (typeof CookieInformation !== 'undefined' && typeof CookieInformation.getConsentGivenFor === 'function') {
240                    return { statistics: CookieInformation.getConsentGivenFor('cookie_cat_statistic'), marketing: CookieInformation.getConsentGivenFor('cookie_cat_marketing') };
241                }
242                return null;
243            }
244
245            function checkOneTrustConsent(activeGroups) {
246                const groups = activeGroups || (typeof OnetrustActiveGroups !== 'undefined' ? OnetrustActiveGroups : null);
247                if (groups) {
248                    return { statistics: groups.includes("2"), marketing: groups.includes("4") };
249                }
250                return null;
251            }
252
253            function checkCookieYesConsent(acceptedCategories) {
254                if (acceptedCategories) {
255                    return { statistics: acceptedCategories.includes("analytics"), marketing: acceptedCategories.includes("advertisement") };
256                } else if (typeof getCkyConsent === 'function' && getCkyConsent()?.categories?.analytics !== undefined) {
257                    const consent = getCkyConsent();
258                    return { statistics: consent.categories.analytics, marketing: consent.categories.advertisement };
259                }
260                return null;
261            }
262
263            function checkCookieFirstConsent(consentData) {
264                const consent = consentData || (typeof CookieFirst !== 'undefined' ? CookieFirst.consent : null);
265                if (consent) {
266                    return { statistics: consent.performance, marketing: consent.advertising };
267                }
268                return null;
269            }
270
271            function checkCookieScriptConsent() {
272                if (typeof CookieScript !== 'undefined' && CookieScript.instance?.currentState) {
273                    const categories = CookieScript.instance.currentState().categories || [];
274                    return { statistics: categories.includes("performance"), marketing: categories.includes("targeting") };
275                }
276                return null;
277            }
278
279            function checkGoogleConsentMode() {
280                if (window.google_tag_data?.ics?.entries) {
281                    const entries = window.google_tag_data.ics.entries;
282                    return { statistics: entries.analytics_storage?.update === 'granted', marketing: entries.ad_storage?.update === 'granted' };
283                }
284                return null;
285            }
286
287            function checkMagentoNativeConsent() {
288                const consent = customerData.get('cookie-status')();
289                if (consent && typeof consent.allowed_cookies !== 'undefined') {
290                    const isGranted = consent.allowed_cookies.length > 0;
291                    return { statistics: isGranted, marketing: isGranted };
292                }
293                return null;
294            }
295
296            function processConsentChange(statisticsConsent, marketingConsent) {
297                const hasStatistics = !!statisticsConsent;
298                const hasMarketing = !!marketingConsent;
299                if (statisticsConsent === undefined && marketingConsent === undefined) return;
300                if (window.cc_statistics === hasStatistics && window.cc_marketing === hasMarketing) return;
301                window.cc_statistics = hasStatistics;
302                window.cc_marketing = hasMarketing;
303                init();
304            }
305
306            function initializeConsentListeners() {
307                const consentPlatformConfigs = {
308                    CookiebotOnConsentReady: { handler: () => { const c = checkCookiebotConsent(); if (c) processConsentChange(c.statistics, c.marketing); } },
309                    CookieInformationConsentGiven: { handler: () => { const c = checkCookieInformationConsent(); if (c) processConsentChange(c.statistics, c.marketing); } },
310                    OneTrustGroupsUpdated: { handler: (e) => { const c = checkOneTrustConsent(e.detail || []); if (c) processConsentChange(c.statistics, c.marketing); } },
311                    cookieyes_consent_update: { handler: (e) => { const c = checkCookieYesConsent(e.detail.accepted || []); if (c) processConsentChange(c.statistics, c.marketing); }, target: document },
312                    cf_consent: { handler: (e) => { const c = checkCookieFirstConsent(e.detail || {}); if (c) processConsentChange(c.statistics, c.marketing); } },
313                    cf_consent_loaded: { handler: (e) => { const c = checkCookieFirstConsent(e.detail || {}); if (c) processConsentChange(c.statistics, c.marketing); } },
314                    'CookieScriptCategory-strict': { handler: () => { const c = checkCookieScriptConsent(); if (c) processConsentChange(c.statistics, c.marketing); } }
315                };
316
317                for (const [eventName, config] of Object.entries(consentPlatformConfigs)) {
318                    (config.target || window).addEventListener(eventName, config.handler);
319                }
320
321                if (window.google_tag_data?.ics?.addListener) {
322                    const processGoogleConsent = () => { const c = checkGoogleConsentMode(); if (c) processConsentChange(c.statistics, c.marketing); };
323                    window.google_tag_data.ics.addListener(["ad_storage", "anal
323ytics_storage"], processGoogleConsent);
324                }
325
326                const magentoConsentObservable = customerData.get('cookie-status');
327                magentoConsentObservable.subscribe(function (consentData) {
328                    if (consentData && typeof consentData.allowed_cookies !== 'undefined') {
329                        const isGranted = consentData.allowed_cookies.length > 0;
330                        processConsentChange(isGranted, isGranted);
331                    }
332                });
333            }
334
335            function runInitialConsentCheck() {
336                let consentDeterminedByCmp = false;
337                const cmpChecks = [
338                    checkMagentoNativeConsent, checkCookiebotConsent, checkCookieInformationConsent,
339                    checkOneTrustConsent, checkCookieYesConsent, checkCookieFirstConsent,
340                    checkCookieScriptConsent, checkGoogleConsentMode
341                ];
342
343                for (const check of cmpChecks) {
344                    const consent = check();
345                    if (consent) {
346                        processConsentChange(consent.statistics, consent.marketing);
347                        consentDeterminedByCmp = true;
348                        break;
349                    }
350                }
351
352                if (!consentDeterminedByCmp) {
353                    const previousDecision = localStorage.getItem(CONSENT_STORAGE_KEY);
354                    if (!('blockScriptBeforeConsent' in window) || !window.blockScriptBeforeConsent || previousDecision === 'true') {
355                        processConsentChange(true, true);
356                    }
357                }
358            }
359
360            function startTracking() {
361                initializeConsentListeners();
362                runInitialConsentCheck();
363            }
364
365            // Handle both cases: DOM already loaded or still loading
366            if (document.readyState === 'loading') {
367                window.addEventListener("DOMContentLoaded", startTracking);
368            } else {
369                startTracking();
370            }
371
372            window.load_pmTPTrack = init;
373        })();
374    }
375);

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.