PageSourceSearch

https://drespen.com/wp-content/plugins/handl-utm-grabber-v3/js/handl-utm-grabber.js?ver=7.1.2

js drespen.com collected 2026-10-06 13:02:34 UTC 18,677 bytes, 541 lines download raw bytes

1// (function(history){
2//     var replaceState = history.replaceState;
3//     history.replaceState = function(state) {
4//         if (typeof history.onreplacestate == "function") {
5//             history.onreplacestate({state: state});
6//         }
7//         return replaceState.apply(history, arguments);
8//     };
9// })(window.history);
10//
11// window.onpopstate = history.onreplacestate = function(e) {
12//     var domain = getDomainName()
13//     console.log(e.state.path)
14//     Cookies.set('handl_url', e.state.path, {
15//         expires: parseInt(handl_utm_cookie_duration[0]),
16//         domain: domain,
17//         sameSite:'None',
18//         secure: true }
19//     );
20// }
21
22var qvars = getUrlVars()
23var domain = getDomainName()
24
25jQuery(function($) {
26    if (handl_utm_cookie_duration[1] === '1' &&
27        typeof(TVE) === "undefined" //thrive editor fix
28    ){
29        RunHandL()
30    }
31});
32
33
34function RunHandL(){
35    window.utms_js = {}
36    window.gaNames = []
37
38    //simulate referrer locally
39    // Object.defineProperty(document, "referrer", {get : function(){ return "https://google.com"; }});
40
41    SetRefLink('handlID', Math.floor(Math.random() * Date.now()), false, 0)
42
43    GAClientID()
44    setHandLParams()
45
46    // console.log("after handl params")
47
48    RunFieldFiller()
49
50    jQuery.each(handl_utm_predefined, function( i,v ) {
51        let cookie_name = v.name;
52        let value = v.value
53
54        if ( value.startsWith('[') && value.endsWith(']') ){ //it's a shortcode!
55            value = v.value.replace(/^\[|\]$/g,'');
56        }else{ //it is a hardcoded default value
57            value = cookie_name
58        }
59
60        if ( ['_ga','gaclientid'].indexOf(value) > -1 ){
61            gaNames.push(cookie_name)
62        }
63
64        if (cookie_name != ''){
65            var cookie_field = GetQVars(value,qvars)
66
67            if (cookie_field == ''){
68                cookie_field = Cookies.get(value)
69            }
70
71            if ( cookie_field != '' && cookie_field != 'PANTHEON_STRIPPED' ){
72                utms_js[cookie_name] = cookie_field
73                SetRefLink(cookie_name, cookie_field, true, 0)
74            }
75        }
76    })
77
78    populateLinks()
79
80    jQuery(document).on( 'nfFormReady', function() {
81        form.fields.map(item => {
82            if (item.default) {
83                var matches = String(item.default).match(/^{(\w+):(\w+)/)
84                if (matches && matches.length == 3) {
85                    let key = matches[1]
86                    let value = matches[2]
87
88                    if (key === 'handl') {
89                        jQuery('#nf-field-' + item.id).val(Cookies.get(value))
90                    }
91                }
92            }
93        })
94
95        //hide hidden fields
96        // jQuery('.nf-field-container.hidden-container').map( (i, item) => jQuery(item).parents().eq(4).hide())
97    })
98}
99
100function RunFieldFiller(){
101
102    Cookies.set('user_agent', navigator.userAgent, {domain: domain})
103
104    jQuery.each(handl_utm_all_params, function( i,vi ) {
105        var cookie_field = GetQVars(vi,qvars)
106
107        var cookie_params = [vi]
108        if ( handl_utm_first_touch_params.includes(vi) )
109            cookie_params.push('first_'+vi)
110
111        cookie_params.forEach( (v, ind) => {
112
113            if ( cookie_field != '' && cookie_field != 'PANTHEON_STRIPPED' ){
114                if ( v.startsWith('first_') && Cookies.get(v) != undefined ){
115                            //do not update the first attributes...
116                }else{
117                    Cookies.set(v, cookie_field, {
118                    expires: parseInt(handl_utm_cookie_duration[0]),
119                    domain: domain,
120                    sameSite:'None',
121                    secure: true });
122                }
123            }
124
125            var curval = decodeURI(Cookies.get(v))
126            curval = curval != 'undefined' ? curval : ''
127            if (curval != 'undefined') {
128                utms_js[v] = curval
129                curval = decodeURIComponent(curval)
130                // curval = curval.replace(/[%]/g,' ')
131                if (v == 'username') {
132                    //Maybe this should apply to all... We'll see...
133                    curval = curval.replace(/\+/g, ' ')
134                }
135
136                jQuery('input[name=\"'+v+'\"]').val(curval)
137                jQuery('input#'+v).val(curval)
138                jQuery('input.'+v).val(curval)
139                jQuery('input#form-field-'+v).val(curval)
140
141                //for nested input fix
142                jQuery('#'+v).find('input').val(curval)
143                jQuery('.'+v).find('input').val(curval)
144
145                jQuery("[data-original_id='"+v+"']").val(curval)
146
147                //wildcard selector
148                jQuery("[name*="+v+"]").val(curval)
149                jQuery("[id*="+v+"]").val(curval)
150                jQuery("[class*="+v+"]").val(curval)
151
152                jQuery("[class*="+v+"_out]").html(curval)
153                jQuery("[id*="+v+"_out]").html(curval)
154
155            }
156        })
157    });
158}
159
160function populateLinks(){
161    jQuery('.utm-out-js, .utm-out-js a').each(function(){
162        var merged_raw = jQuery.extend( {}, utms_js )
163        var merged = Object.keys(merged_raw)
164            .filter(key => handl_utm_append_params.includes(key))
165            .reduce((obj, key) => {
166                obj[key] = merged_raw[key];
167                return obj;
168            }, {});
169        merged = jQuery.extend(merged, getSearchParams(this.href))
170        var current_page = window.location.href.split('?')[0]
171        if (
172            this.href !== undefined &&
173            this.href !== "" &&
174            !this.href.match("#") &&
175            !this.href.startsWith("tel:") &&
176            !this.href.startsWith("mailto:") &&
177            !this.href.startsWith("sms:") &&
178            !jQuery(this).attr('onclick') &&
179            this.href.match(new RegExp("^"+current_page+"$")) == null
180        ){
181            var href = this.href.split("?")[0];
182            if ( !jQuery.isEmptyObject(merged) )
183                this.href = href+"?"+decodeURIComponent(jQuery.param(merged))
184        }
185    })  ;
186
187    // console.log(handl_utm)
188    // console.log(utms_js)
189
190    jQuery('.utm-out, .utm-out a').each(function(){
191        var merged_raw = jQuery.extend( {}, handl_utm, utms_js )
192        var merged = Object.keys(merged_raw)
193            .filter(key => handl_utm_append_params.includes(key))
194            .reduce((obj, key) => {
195                if (merged_raw[key]) {
196                    obj[key] = decodeURIComponent(merged_raw[key]);
197                }
198                return obj
199            }, {});
200        merged = jQuery.extend(merged, getSearchParams(this.href))
201        var current_page = window.location.href.split('?')[0]
202        if (
203            this.href !== undefined &&
204            this.href !== "" &&
205            //             !this.href.match("#") &&
206            !this.href.startsWith("tel:") &&
207            !this.href.startsWith("mailto:") &&
208            !this.href.startsWith("sms:") &&
209            // !jQuery(this).attr('onclick') &&
210            this.href.match(new RegExp("^"+current_page+"$")) == null
211        ){
212            var href = this.href.split("?")[0];
213            var parts = []
214            if (this.href.match("#")){
215                parts = this.href.split("#")
216                href = parts[0]
217            }
218
219            // console.log(parts, href)
220            if ( !jQuery.isEmptyObject(merged) && !this.href.startsWith(current_page) ){
221                if (this.href.match("#") && parts.length == 2){
222                    this.href = href+"?"+decodeURIComponent(jQuery.param(merged))+"#"+parts[1].split("?")[0]
223                }else{
224                    this.href = href+"?"+decodeURIComponent(jQuery.param(merged))
225                }
226            }
227        }
228    });
229
230    jQuery('.utm-src').each(function(){
231        var target_url
232        if (this.src){
233            target_url = this.src
234        }else if (jQuery(this).data('url')){
235            target_url = jQuery(this).data('url')
236        }
237
238        var merged_raw = jQuery.extend( {}, handl_utm, utms_js )
239        var merged = Object.keys(merged_raw)
240            .filter(key => handl_utm_append_params.includes(key))
241            .reduce((obj, key) => {
242                if (merged_raw[key]) {
243                    obj[key] = decodeURIComponent(merged_raw[key]);
244                }
245                return obj;
246            }, {});
247        merged = jQuery.extend( merged, getSearchParams(target_url) )
248
249        var src = target_url.split("?")[0];
250        if ( !jQuery.isEmptyObject(merged) ){
251            var final_target = src + "?" + decodeURIComponent(jQuery.param(merged))
252            if (this.src) {
253                this.src = final_target
254            }else if (jQuery(this).data('url')){
255                jQuery(this).data('url', final_target)
256            }
257        }
258    });
259}
260
261function getSearchParams(url,k){
262    var p={};
263    var a = document.createElement('a');
264    a.href = url;
265    a.search.replace(/[?&]+([^=&]+)=([^&]*)/gi,function(s,k,v){p[k]=v})
266    return k?p[k]:p;
267}
268
269function GetQVars(v,qvars){
270    if (qvars[v] != undefined) {
271        return qvars[v]
272    }
273    return ''
274}
275
276function getUrlVars() {
277    var vars = {};
278    window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
279        vars[key] = value;
280    });
281    return vars;
282}
283
284function GAClientID(){
285    if ( typeof(ga) == 'function' && typeof(ga.getAll) == 'function'){
286        var trackers = ga.getAll();
287        if (trackers.length > 0 ){
288            //maybe we'll update this later, so we can loop all the tracking ids...
289            var entries = Object.entries({"clientId":"gaclientid","referrer":"organic_source"})
290            var domain = getDomainName()
291            for (const [gaName, handlName] of entries) {
292                var gaValue = trackers[0].get(gaName)
293                if (gaValue !== undefined) {
294                    if (gaName != 'clientId'){ // we should reset the array for anythng other than clientId
295                        gaNames = []
296                    }
297
298                    gaNames.push(handlName)
299
300                    for (const curName of gaNames){
301                        // console.log(curName, gaValue)
302                        utms_js[curName] = gaValue
303                        Cookies.set(curName, gaValue, {
304                            expires: parseInt(handl_utm_cookie_duration[0]),
305                            domain: domain,
306                            sameSite:'None',
307                            secure: true }
308                        );
309                        // console.log(`Setting ${handlName} as ${gaValue}`)
310                        jQuery('input[name=\"' + curName + '\"]').val(gaValue)
311                        jQuery('input#' + curName).val(gaValue)
312                        jQuery('input.' + curName).val(gaValue)
313                    }
314
315                    // so we can include gaclientid to the links incase needed
316                    populateLinks()
317                }
318            }
319        }
320    }else{
321        setTimeout(GAClientID,500);
322    }
323}
324
325function getDomainName(){
326    var name="HandLtestDomainName"
327    var value="HandLtestDomainValue"
328    var host=location.host
329    var domain;
330
331    if (host.split('.').length === 1){
332        domain = '';
333    }else{
334        var domainParts = host.split('.');
335        domainParts.shift();
336        domain = '.'+domainParts.join('.');
337        Cookies.set(name, value, {domain: domain})
338        if (Cookies.get(name) == null || Cookies.get(name) != value)
339        {
340            domain = '.'+host;
341        }
342    }
343
344    return domain;
345}
346
347function setHandLParams(){
348    // console.log("set handl params")
349
350    SetRefLink('handl_url_base', document.location.origin + document.location.pathname, true, 0)
351    SetRefLink('handl_url', document.location.href, true, 0)
352    SetRefLink('handl_ref', document.referrer, true, 0)
353    SetRefLink('handl_ref_domain', document.referrer == '' ? '' : this.get_url_domain(document.referrer), true, 0)
354    SetRefLink('handl_landing_page', document.location.href, false, 0)
355    SetRefLink('handl_original_ref', document.referrer, false, 0)
356
357    let original_ref =  Cookies.get('handl_ref_domain')
358    let this_domain = document.location.host
359    let source = "Other";
360    if (original_ref == '') {
361        source = "Direct";
362    }else if ( original_ref.match(/google/i) !== null ){
363        source = "Google";
364    }else if ( original_ref.match(/yahoo/i) !== null ){
365        source = "Yahoo";
366    }else if ( original_ref.match(/bing/i) !== null ){
367        source = "Bing";
368    }else if ( original_ref.match(/instagram/i) !== null ){
369        source = "Instagram";
370    }else if ( original_ref.match(/facebook/i) !== null || original_ref.match(/fb\.com$/i) !== null ){
371        source = "Facebook";
372    }else if ( original_ref.match(/twitter/i) !== null || original_ref.match(/t\.co$/i) !== null ){
373        source = "Twitter";
374    }else if ( original_ref.match(/snapchat/i) !== null ){
375        source = "Snapchat";
376    }else if ( original_ref.match(/youtube/i) !== null ){
377        source = "YouTube";
378    }else if ( original_ref.match(/pinterest/i) !== null ){
379        source = "Pinterest";
380    }else if ( original_ref.match(/linkedin/i) !== null ){
381        source = "LinkedIn";
382    }else if ( original_ref.match(/tumblr/i) !== null ){
383        source = "Tumblr";
384    }else if ( original_ref.match(/duckduckgo/i) !== null ){
385        source = "Duckduckgo";
386    }else if (this_domain == original_ref){
387        source = "Internal";
388    }
389
390    if(
391        ( !Cookies.get('organic_source_str') || Cookies.get('organic_source_str') == '' ) ||
392        ( Cookies.get('organic_source_str') && Cookies.get('organic_source_str') != '' && ['Internal','Direct'].indexOf(source) == -1 )
393    ){
394        SetRefLink('organic_source', document.referrer, true, 0)
395        SetRefLink('organic_source_str', source, true, 0)
396    }
397
398
399    var qvars = getUrlVars()
400    let traffic_source = 'Other'
401    if (
402        Cookies.get('fbclid') != undefined ||
403        Cookies.get('gclid') != undefined ||
404        Cookies.get('msclkid') != undefined ||
405        Object.keys(qvars).indexOf('fbclid') != -1 ||
406        Object.keys(qvars).indexOf('gclid') != -1 ||
407        Object.keys(qvars).indexOf('msclkid') != -1
408    ){
409        traffic_source = 'Paid'
410    }
410else if ( ['Google','Bing','Yahoo','Duckduckgo'].indexOf(Cookies.get('organic_source_str')) > -1 ){
411        traffic_source = 'Organic'
412    }else if ( ['Facebook','Twitter','Instagram','Snapchat','YouTube','Pinterest','LinkedIn','Tumblr'].indexOf(Cookies.get('organic_source_str')) > -1 ){
413        traffic_source = 'Social'
414    }else if ( ['Internal','Direct'].indexOf(Cookies.get('organic_source_str')) > -1 ){
415        traffic_source = 'Direct'
416    }else if ( Cookies.get('organic_source_str') && ['Internal'].indexOf(Cookies.get('organic_source_str')) == -1 ){
417        traffic_source = 'Referral'
418    }
419
420    SetRefLink('traffic_source', traffic_source, true, 0)
421    SetRefLink('first_traffic_source', traffic_source, false, 0)
422
423}
424
425function SetRefLink(field, value, overwrite, count){
426    // console.log(`Trying ${count} for ${field} ---> ${value}`)
427    var domain = getDomainName()
428
429    if (Cookies.get(field) != value) {
430        if (Cookies.get(field) !== undefined && Cookies.get(field) != "" && !overwrite ){
431            //bail...
432            // console.log(`No need to update... ${field}`)
433        }else if (Cookies.get(field) === undefined || Cookies.get(field) == '' || overwrite) {
434            if (count == undefined) {
435                count = 0
436            }
437
438            // utms_js[field] = value //not sure if we really need it here
439            // console.log(`Setting cookies for ${field} as ${value}`)
440            Cookies.set(field, value, {
441                    expires: parseInt(handl_utm_cookie_duration[0]),
442                    domain: domain,
443                    sameSite: 'None',
444                    secure: true
445                }
446            );
447
448        } else {
449            count = count + 1
450            if (count < 3) {
451                setTimeout(function () {
452                    SetRefLink(field, value, overwrite, count)
453                }, 500)
454            }
455        }
456    }
457}
458
459function get_url_domain(url) {
460    let a      = document.createElement('a');
461    a.href = url;
462    return a.hostname;
463}
464
465//This is for OptinMonster events: https://optinmonster.com/docs/optinmonster-javascript-events-api/
466document.addEventListener('om.Campaign.afterShow',function(){
467    RunFieldFiller()
468})
469
470window.addEventListener('CookiebotOnAccept', function (e) {
471    if (Cookiebot.consent.marketing)
472    {
473        RunHandL()
474    }
475}, false);
476
477document.addEventListener("cmplz_enable_category", function(consentData) {
478    var category = consentData.detail.category;
479    if (category==='marketing'){
480        RunHandL()
481    }
482});
483
484
485function afterBookingSelectDateAndTime(a){
486    Object.keys(a.bookings[0].customFields).forEach(function(key) {
487        let label = a.bookings[0].customFields[key].label
488        if (handl_utm[label]){
489            a.bookings[0].customFields[key].value = handl_utm[label]
490        }
491    });
492}
493
494window.ameliaActions = {
495    beforeBooking: function(x,y,a){
496        afterBookingSelectDateAndTime(a)
497        x()
498    }
499}
500
501/*
502FormStack solution...
503 */
504function FF_AutoFiller(v){
505    it = jQuery(v)
506
507    for ( vv of ['placeholder', 'title'] ) {
508        var ph = it.attr(vv)
509        var n = handl_utm_all_params.findIndex( (p) => new RegExp(p,'i').test(ph) )
510        if (n > -1){
511            cval = Cookies.get(handl_utm_all_params[n])
512            it.val(cval).attr('value',cval)
513        }
514    }
515}
516setTimeout(function () {
517    jQuery('.fsCell.fsHidden>input').map( (i, v) => { FF_AutoFiller(v)  })
518}, 3000)
519
520jQuery(document).on('wsf-rendered', function(event, form_object, form_id, instance_id) {
521    RunFieldFiller()
522});
523
524// function FF_OnAfterRender(){
525//     console.log("Form rendered successfully!");
526//     // var vars = ["Lead.UTM_Campaign__c","Lead.UTM_Medium__c","Lead.UTM_Source__c","Lead.UTM_Term__c","Lead.UTM_Content__c","Lead.GCLID__c","Lead.FBclid__c"]
527//     // for (v of vars){
528//     //     escape_v = v.replace(/\./g, '\\.')
529//     //     jQuery("#"+escape_v).val(Cookies.get(v));
530//     // }
531//     return true;
532// }
533
534// wp.hooks.addFilter('jet.fb.inputs','jet-plugins',function(x,y,z,t,i){console.log(x,y,z,t,i)})
535// wp.hooks.addFilter('jet.fb.macro.field.value','jet-plugins',function(x,y,z,t,i){console.log(x,y,z,t,i)})
536// wp.hooks.addAction('jet.fb.observe.after','jet-plugins',function(x,y,z,t,i){console.log(x,y,z,t,i)})
537
538//This is to support Jet Form Builder
539jQuery(document).on('jet-form-builder/after-init',function(e){
540    RunFieldFiller()
541})

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.