简体   繁体   中英

remove email from URL string Javascript

My intention

My intention is to NOT let the Facebook pixel receive the Email that is mentioned in the URL string that is the main goal. I would like to solve this by using, for example, Custom Javascript in Google Tag Manager before the Facebook Pixel has been fired.

My question

I am trying to remove the email portion from the URL being given to Facebook through the Facebook Pixel. I am not a developer and the Facebook Pixel is used through GTM. I can see that the above string fires on for example location. href, document.location.href and window.location.href

My problem

URL string that i'd like to modify (note I have removed sensitive parts from the URL):

https://booking.company.com/#/no/payment/accepted?postype=ssl&currencynumber=578&authkey=4fe55a1aeb572cc6a252513f8f16d3ac&accepturl=https:%2F%2Fbooking.company.com%2Fbservice%2Fno%2FPaymentReturn%2FRedirect%2Faccepted&amount=5900&decorator=responsive&orderid=dontmindme&merchant=dontmindme&ordertext=Booking%20nr:%209689228&flexwin_cardlogosize=1&statuscode=12&transact=dontmindme&cardnomask=dontmindme&cardexpdate=dontmindme&capturenow=1&currency=578&md5key=022b61cc5d7d3937e2db03b91aa51559&callbackurl=https:%2F%2Fjava-prod-flc.carus.com%2Fcardibsd2%2FDibsD2%2FPayResp&currencyalpha=NOK&lang=no&paytype=VISA&cancelurl=https:%2F%2Fbooking.company.com%2Fbservice%2Fno%2FPaymentReturn%2FRedirect%2Fcancelled& email=remove@this.com

Code

function removeEmail(url, window.location.href) {

        var urlparts = url.split('&Email');{

        var prefix = encodeURIComponent(window.location.href) + '&Email';
        var pars = urlparts[1].split(/[&;]/g);


        return urlparts[0];
    }
    return url;
}

EDIT (20.02.2020):
- Made some adjustments based on feedback
- Added code (code is based from this question: How can I delete a query string parameter in JavaScript? )

I am aware that the best solution is to fix it in the backend, but the fix in the backend will take a very long time, hence I am looking for a temporary solution in order to be compliant with GDPR, etc.

window.location.hash = window.location.hash.replace(/&email=[^&]+/, '')) should do the trick.

That will replace &email=… in the URL hash value with an empty string, and assign that as the new hash.

As long as this happens before the Facebook pixel gets initialized, it should make it read the URL without the email value in it now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM