0

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.

Steven
  • 23
  • 5
  • Please go read [ask]. You said you were trying - and we want to _see_ your actual try. – CBroe Feb 20 '20 at 08:41
  • @CBroe; Thx for the feedback. I take it seriously and i've made some adjustments to the structure + added code. – Steven Feb 20 '20 at 09:09
  • If it is lowercase `email` in the URL, then you’ll have to split using the lowercase spelling as well. And `encodeURIComponent` applied to the whole URL makes little sense, that will probably just invalidate most of it. – CBroe Feb 20 '20 at 09:14
  • Might make more sense to operate on `window.location.hash` to begin with. – CBroe Feb 20 '20 at 09:15
  • @CBroe using window.location.hash might make more sense indeed. I would need to test it, but as it stands now the code isn't functioning correctly and isn't removing the email as it's intended to do. – Steven Feb 20 '20 at 09:26
  • `window.location.hash = window.location.hash.replace(/&email=[^&]+/, ''))` should do the trick. – CBroe Feb 20 '20 at 09:45
  • Your current function only seems to return the new URL, but it actually needs to get assigned back somewhere. – CBroe Feb 20 '20 at 09:45
  • @cbroe sry for asking, but where in the code would this be implemented? See below. function removeEmail(url, window.location.hash) { var urlparts = url.split('&email');{ var prefix = window.location.hash = window.location.hash.replace(/&email=[^&]+/, '')) var pars = urlparts[1].split(/[&;]/g); return urlparts[0]; } return url; } – Steven Feb 20 '20 at 09:49
  • That does the whole job already, it doesn’t need to be integrated into anything else. You don’t even need to pass any parameters into any function, `window.location.hash` is globally available. – CBroe Feb 20 '20 at 09:51
  • Thx for explaining. So how about this - Would this work? `function () { return window.location.hash = window.location.hash.replace(/&email=[^&]+/, '')) }` – Steven Feb 20 '20 at 09:55
  • Doesn’t need to `return` anything. (And that function needs to be called somewhere, of course. Not sure how exactly you do that within GTM. Maybe it doesn’t even need to be inside a function, but you can just place it there as a single line of code, before the stuff that actually embeds the FB pixel?) – CBroe Feb 20 '20 at 10:02
  • @cbroe good thoughts, i will test it right away. will give you the result shortly. – Steven Feb 20 '20 at 10:06
  • Since FB collects the url from the browser address bar, you probably need a custom tag that removes the offending bit, pushes the cleaned url to the browser history, and on its success event pushes a value to the datalayer that fires the FB tag. Else facebook will have access to the complete url regardless. – Eike Pierstorff Feb 20 '20 at 10:46
  • @EikePierstorff I tried to search for where exactly FB gets the URL from. I would guess it's either href, hash or something else. Your theory seems logical. My guess is that i fire either the code above in the comments within the same script as facebook, or fires the script Before Facebook fires. There's an option in GTM to fire a tag before a tag. So that seems like the most logical idea in order to be safe. But this will affect everyother tag. Or is there anything else you suggest me to try out? – Steven Feb 20 '20 at 11:10
  • Quick questions @CBroe this code, does this return anything or just look it up / remove the information? – Steven Feb 20 '20 at 11:37
  • The line that I have shown just reads the current hash value, cuts off the `&email=…` part, and then assigns this as the new hash. If you just to go your page with that parameter in the URL, and then execute this line via the browser console ([F12]), you should see it working already, the address bar content should have changed after that. How to properly stuff this into GTM to get the desired result though, not exactly sure. Maybe you can show us how exactly the pixel gets embedded via GTM in the first place. – CBroe Feb 20 '20 at 11:42
  • 1
    After some real life testes the solution is working as intended. I didn't remove or edit anything. What i did was 1. Making sure that i had a seperate Custom HTML Tag with to fire Before FB Pixel with Tag Sequencing .. then 2.fire the FB pixel, the email will then be removed BEFORE the pixel fires so facebook has no clue that it's there and won't register it in their systems. Thanks for your patience with a guy who really wants to know coding but does not know how to code well. although i understand the logic. So.. how do i mark this as solved? – Steven Feb 20 '20 at 12:11
  • @Steven glad it worked for you. Marking as solved happens by accepting an answer here on SO, so I added a short one that contains the main thing developed during this discussion. – CBroe Feb 21 '20 at 07:28
  • @CBroe Perfect :) – Steven Feb 21 '20 at 13:45

1 Answers1

0

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.

CBroe
  • 91,630
  • 14
  • 92
  • 150