PageSourceSearch

https://www.terravision.eu/wp-content/themes/TvisionNew/js/tracking.cookie.js

js terravision.eu collected 2026-09-24 09:15:47 UTC 9,304 bytes, 260 lines download raw bytes

1/**
2 * Terravision Affiliate Tracking Cookie Management System
3 * 
4 * This script handles affiliate marketing tracking by parsing URL parameters
5 * and making API calls to set/delete tracking cookies. It supports both
6 * internal Terravision affiliate tracking and third-party partner tracking.
7 * 
8 * @author Terravision Development Team
9 * @version 1.0
10 * @requires jQuery
11 * @requires jQuery.cookie plugin
12 */
13
14jQuery(function($) {
15    /**
16     * Cross-browser AJAX GET request handler
17     * 
18     * Provides compatibility for older browsers using Microsoft XDomainRequest
19     * and falls back to jQuery AJAX for modern browsers. Handles CORS requests.
20     * 
21     * @param {string} url - The URL to make the GET request to
22     * @param {Object} data - Query parameters to append to the URL
23     * @param {Function} callback - Callback function to handle the response
24     * @param {string} [type] - Expected response type (optional)
25     * 
26     * @example
27     * ajaxGet('/api/tracking', {id: 123}, function(response) {
28     *     console.log('Response:', response);
29     * });
30     */
31    ajaxGet = function (url, data, callback, type) {
32        // try XDR
33        if (window.XDomainRequest) {
34            var params = '';
35            for (var key in data) {
36                params += ((params || url.indexOf("?") != -1)?'&':'?')+key+'='+data[key];
37            }
38            // Use Microsoft XDR
39            // Use Microsoft XDR
40            var xdr = new XDomainRequest();
41            xdr.open("GET", url+params);
42            xdr.onload = function() {
43                callback(JSON.parse(this.responseText), 'success');
44            };
45            xdr.send();
46        } else {
47            try {
48                // Try using jQuery to get data
49                $.get(url, data, callback, type);
50            } catch(e) {
51                alert(e.message);
52            }
53        }
54    };
55
56    /**
57     * URL Parameter Tracking Manager
58     * 
59     * Parses and manages URL query string parameters for tracking purposes.
60     * Provides methods to check for specific parameters and retrieve their values.
61     * 
62     * @constructor
63     * @example
64     * var tcm = new TrackingCookieManager();
65     * tcm.initialize();
66     * if (tcm.has_param('source')) {
67     *     var source = tcm.get_param_value('source');
68     * }
69     */
70    TrackingCookieManager = function(){
71
72        /** @type {Object} - Key-value pairs of URL parameters */
73        var querystring_parameters = [];
74        /** @type {Array} - Array of parameter names only */
75        var plain_params = [];
76        /** @type {string|null} - Currently matched parameter */
77        var matched_param = null;
78
79        /**
80         * Initialize the tracking manager by parsing current URL parameters
81         * 
82         * Extracts all query string parameters from window.location.search
83         * and stores them in both key-value and plain parameter formats.
84         * 
85         * @example
86         * // For URL: ?source=google&campaign=summer2023
87         * tcm.initialize();
88         * // querystring_parameters = {source: 'google', campaign: 'summer2023'}
89         * // plain_params = ['source', 'campaign']
90         */
91        this.initialize = function(){
92
93            var params = window.location.search.substr(1).split('&');
94            if (params.length >= 0){
95                $.each(params, function(idx, el){
96                        querystring_parameters[el.split("=")[0]] = el.split("=")[1];
97                        plain_params.push(el.split("=")[0]);
98                    });
99            }
100        };
101
102        /**
103         * Check if at least one parameter from the provided array exists in the URL
104         * 
105         * @param {Array} params - Array of parameter names to check
106         * @returns {string|null} - The first matching parameter name, or null if none found
107         * 
108         * @example
109         * var param = tcm.has_at_least_one(['source', 'zanpid', 'tduid']);
110         * if (param) {
111         *     console.log('Found parameter:', param);
112         * }
113         */
114        this.has_at_least_one = function(params){
115            matched_param = null;
116            $.each(params,function(idx, el){
117                if ($.inArray(el,plain_params) != -1) {
118                    matched_param = el;
119                }
120            });
121            return matched_param;
122        };
123
124        /**
125         * Check if a specific parameter exists in the URL
126         * 
127         * @param {string} param - Parameter name to check
128         * @returns {boolean} - True if parameter exists, false otherwise
129         * 
130         * @example
131         * if (tcm.has_param('source')) {
132         *     console.log('Source parameter found');
133         * }
134         */
135        this.has_param = function(param){
136            return querystring_parameters[param] ? true : false;
137        };
138
139        /**
140         * Get the value of a specific URL parameter
141         * 
142         * @param {string} param - Parameter name to retrieve
143         * @returns {string|undefined} - Parameter value or undefined if not found
144         * 
145         * @example
146         * var source = tcm.get_param_value('source');
147         * console.log('Source value:', source);
148         */
149        this.get_param_value = function(param){
150            return querystring_parameters[param];
151        };
152    };
153
154    // Initialize tracking manager
155    tcm = new TrackingCookieManager();
156    tcm.initialize();
157    $.support.cors = true;
158
159    /**
160     * Cookie Handler for Tracking Responses
161     * 
162     * Processes server responses and sets/deletes cookies based on the response data.
163     * Expects a JSON response with cookie configuration details.
164     * 
165     * @param {Object} data - Server response data containing cookie configuration
166     * @param {string} data.cookie_name - Name of the cookie to set
167     * @param {string} data.cookie_value - Value to store in the cookie
168     * @param {string} data.cookie_domain - Domain for the cookie
169     * @param {number} data.cookie_days - Expiration time in days
170     * @param {Array} data.cookies_delete - Array of cookie names to delete
171     * @param {string} status - Response status (not used)
172     * 
173     * @example
174     * // Expected server response:
175     * {
176     *     cookie_name: 'affiliate_tracking',
177     *     cookie_value: 'aff_123_camp_456',
178     *     cookie_domain: '.terravision.eu',
179     *     cookie_days: 30,
180     *     cookies_delete: ['old_tracking_cookie']
181     * }
182     */
183    cookieHandler = function(data, status){
184        // Set the main tracking cookie
185        $.cookie(
186            data.cookie_name,
187            data.cookie_value,
188            {
189                domain: data.cookie_domain,
190                expires: data.cookie_days,
191                path: '/'
192            }
193        );
194        
195        // Also set standardized tracking cookies for form integration
196        if (data.cookie_name && data.cookie_value) {
197            // Set affiliate tracking cookie
198            $.cookie(
199                'affiliate_tracking',
200                data.cookie_value,
201                {
202                    domain: data.cookie_domain,
203                    expires: data.cookie_days,
204                    path: '/'
205                }
206            );
207            
208            // Set source tracking cookie if we have source information
209            if (data.cookie_name.includes('source') || data.cookie_name.includes('partner')) {
210                $.cookie(
211                    'source_tracking',
212                    data.cookie_value,
213                    {
214                        domain: data.cookie_domain,
215                        expires: data.cookie_days,
216                        path: '/'
217                    }
218                );
219            }
220            
221            // Set campaign tracking cookie if we have campaign information
222            if (data.cookie_name.includes('campaign') || data.cookie_name.includes('cm')) {
223                $.cookie(
224                    'campaign_tracking',
225                    data.cookie_value,
226                    {
227                        domain: data.cookie_domain,
228                        expires: data.cookie_days,
229                        path: '/'
230                    }
231                );
232            }
233        }
234        
235        $.each(data.cookies_delete, function(idx, el){
236            //Remove the cookie
237            $.cookie(el, '', { expires: -1, path:'/', domain: data.cookie_domain });
238        });
239    };
240
241    /**
242     * Main Tracking Logic
243     * 
244     * Determines which tracking endpoint to call based on URL parameters:
245     * 1. Internal affiliation tracking (af + cm parameters)
246     * 2. Third-party affiliation tracking (various partner parameters)
247     */
248
249    // Internal Terravision Affiliation Tracking
250    if( tcm.has_param('af') && tcm.has_param('cm')) {
251        var url = internalAffiliationEndpoint + tcm.get_param_value('af') + '/' + tcm.get_param_value('cm');
252        url += window.location.search;
253        ajaxGet(url,{}, cookieHandler);
254
255    // Third-Party Affiliation Tracking
256    }else if(param = tcm.has_at_least_one(['source','zanpid','tduid', '
256yahooid', 'adwd', 'adws','adwy', 'fbad'])) {
257        var url = thirdPartAffiliationEndpoint + param + '/' + tcm.get_param_value(param);
258        ajaxGet(url,{}, cookieHandler);
259    }
260});

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.