PageSourceSearch

https://www.goodenergy.co.uk/wp-content/plugins/handl-utm-grabber/js/handl-utm-grabber.js

js goodenergy.co.uk collected 2026-10-01 10:30:52 UTC 7,552 bytes, 215 lines download raw bytes

1var qvars = getUrlVars()
2var handlConsentGranted = typeof handl_utm_cookie_duration === 'undefined' || handl_utm_cookie_duration[1] === '1'
3
4jQuery(function($) {
5    if (handlConsentGranted) {
6        RunHandL()
7    }
8});
9
10function RunHandL(){
11    handlConsentGranted = true
12    setHandLParams()
13    RunFieldFiller()
14    HandLAppendTrackedLinks()
15    HandLBindNinjaFormsFiller()
16}
17
18// NF inputs (#nf-field-{id}) carry no param name, so the generic selector
19// pass can't reach them; re-fill on nfFormReady ('' when the cookie is absent).
20var handlNfFillerBound = false
21function HandLBindNinjaFormsFiller(){
22    if (handlNfFillerBound) return
23    handlNfFillerBound = true
24
25    jQuery(document).on( 'nfFormReady', function () {
26        if (typeof nfForms === 'undefined') return
27        nfForms.forEach(function (form) {
28            form.fields.map(function (item) {
29                if (item.default) {
30                    var matches = String(item.default).match(/^{(\w+):(\w+)/)
31                    if (matches && matches.length == 3 && matches[1] === 'handl') {
32                        var c = Cookies.get(matches[2])
33                        jQuery('#nf-field-' + item.id).val(c === undefined ? '' : decodeURIComponent(c).replace(/[%]/g,' '))
34                    }
35                }
36            })
37        })
38    })
39}
40
41// IP stays server-side.
42function setHandLParams(){
43    var cookieOptions = { expires: parseInt(handl_utm_cookie_duration[0]) }
44
45    Cookies.set('handl_url', document.location.href, cookieOptions)
46    Cookies.set('handl_ref', document.referrer, cookieOptions)
47
48    if (Cookies.get('handl_landing_page') === undefined) {
49        Cookies.set('handl_landing_page', document.location.href, cookieOptions)
50    }
51
52    if (Cookies.get('handl_original_ref') === undefined) {
53        Cookies.set('handl_original_ref', document.referrer, cookieOptions)
54    }
55}
56
57function HandLAppendTrackedLinks(){
58    jQuery('.utm-out').each(function(){
59        // Only process if this is an anchor tag with href
60        if (this.tagName.toLowerCase() !== 'a' || !this.href) {
61            return;
62        }
63
64        // Sanitize URL parameters and handl_utm object
65        var urlParams = getSearchParams(this.href);
66        var sanitizedParams = {};
67
68        // Only include parameters that exist in handl_utm
69        for(var key in urlParams) {
70            if(handl_utm.hasOwnProperty(key)) {
71                sanitizedParams[key] = encodeURIComponent(urlParams[key]);
72            }
73        }
74
75        // Sanitize handl_utm values
76        var sanitizedHandlUtm = {};
77        for(var key in handl_utm) {
78            sanitizedHandlUtm[key] = encodeURIComponent(handl_utm[key]);
79        }
80
81        // Merge sanitized objects
82        var merged = jQuery.extend({}, sanitizedHandlUtm, sanitizedParams);
83
84        // Reset href and append sanitized parameters
85        this.href = this.href.split('?')[0]; // Keep base URL only
86        if(!jQuery.isEmptyObject(merged)) {
87            this.href += "?" + jQuery.param(merged);
88        }
89    });
90}
91
92// Marketing consent granted -> capture; withdrawn -> clean up. No reload.
93function HandLOnMarketingConsentChange(changedConsentCategory) {
94    for (var key in changedConsentCategory) {
95        if (changedConsentCategory.hasOwnProperty(key)) {
96            if (key === 'marketing' && changedConsentCategory[key] === 'allow') {
97                console.log('Consent: Marketing consent granted, running HandL');
98                RunHandL();
99            } else if (key === 'marketing' && changedConsentCategory[key] === 'deny') {
100                console.log('Consent: Marketing consent withdrawn, removing HandL cookies');
101                RemoveHandLCookies();
102            }
103        }
104    }
105}
106
107// WP Consent API broadcasts (any consent manager, incl. our banner in manager mode).
108document.addEventListener('wp_listen_for_consent_change', function (e) {
109    HandLOnMarketingConsentChange(e.detail);
110});
111
112// Our banner's own event, standalone mode.
113document.addEventListener('handl_consent_change', function (e) {
114    HandLOnMarketingConsentChange(e.detail);
115});
116
117// Server-side cookies carry a leading-dot domain, JS-set ones none; clear every variant.
118function RemoveHandLCookies() {
119    console.log('Removing HandL cookies');
120    var params = typeof handl_utm_all_params !== 'undefined' ? Object.values(handl_utm_all_params) : []
121    var host = location.hostname.toLowerCase().replace(/^www\./, '')
122    params.forEach(function (param) {
123        Cookies.remove(param, { path: '/' })
124        Cookies.remove(param, { path: '/', domain: '.' + host })
125        Cookies.remove(param, { path: '/', domain: host })
126    })
127    handlConsentGranted = false
128}
129
130function RunFieldFiller(){
131    jQuery.each([ 'utm_source','utm_medium','utm_term', 'utm_content', 'utm_campaign', 'gclid', 'handl_landing_page', 'handl_original_ref', 'handl_ip', 'handl_ref', 'handl_url', 'email', 'username' ], function( i,v ) {
132
133        var cookie_field = GetQVars(v,qvars)
134
135        if ( cookie_field != '' )
136            Cookies.set(v, cookie_field, { expires: 30 });
137
138        var curval = Cookies.get(v)
139
140        if (curval != undefined) {
141            curval = decodeURIComponent(curval).replace(/[%]/g,' ')
142
143            if ([ 'utm_source', 'utm_medium', 'utm_term', 'utm_content', 'utm_campaign', 'gclid' ].indexOf(v) !== -1) {
144                handl_utm[v] = curval
145            }
146
147            if (v == 'username') {
148                //Maybe this should apply to all... We'll see...
149                curval = curval.replace(/\+/g, ' ')
150            }
151        } else {
152            // No cookie: blank tracked fields (cached pages carry baked values).
153            // skip email/username
154            if ([ 'email', 'username' ].indexOf(v) !== -1) {
155                return
156            }
157            curval = ''
158        }
159
160        {
161            jQuery('input[name=\"'+v+'\"]').val(curval)
162            jQuery('input#'+v).val(curval)
163            jQuery('input.'+v).val(curval)
164            jQuery('input#form-field-'+v).val(curval)
165
166            //for nested input fix
167            jQuery('#'+v).find('input').val(curval)
168            jQuery('.'+v).find('input').val(curval)
169
170            jQuery("[data-original_id='"+v+"']").val(curval)
171            jQuery("[data-name='"+v+"']").val(curval)
172            jQuery("[data-name='"+v.replace(/_/g,'')+"']").val(curval) //Active Campaign
173            jQuery("[placeholder='"+v+"']").val(curval) //Dyanmics 365 + Generic
174
175            if (v.length > 4 && ['email','username'].indexOf(v) == -1){ // this is for making sure wildcards are not hyper sensitive. (email/username are free-only params, V3 never wildcards them)
176                //wildcard selector
177                jQuery("[name*="+v+"]").val(curval)
178                jQuery("[id*="+v+"]").val(curval)
179                jQuery("[class*="+v+"]").val(curval)
180            }
181
182        }
183    });
184}
185
186function getSearchParams(url,k){
187    var p={};
188    var a = document.createElement('a');
189    a.href = url;
190    a.search.replace(/[?&]+([^=&]+)=([^&]*)/gi,function(s,k,v){p[k]=v})
191    return k?p[k]:p;
192}
193
194function GetQVars(v,qvars){
195    if (qvars[v] != undefined) {
196        return qvars[v]
197    }
198    return ''
199}
200
201function getUrlVars() {
202    var vars = {};
203    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
204        vars[key] = value;
205    });
206    return vars;
207}
208
209jQuery( document ).on( 'elementor/popup/show' , function () {
210    setTimeout(function(){
211        if (handlConsentGranted) {
212            RunFieldFiller()
213        }
214    }, 1000)
215} );

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.