Skip to content

Instantly share code, notes, and snippets.

@soulbliss
Created April 6, 2024 15:20
Show Gist options
  • Save soulbliss/10faaa89ff26d6eb85b7eb24dfc1273a to your computer and use it in GitHub Desktop.
Save soulbliss/10faaa89ff26d6eb85b7eb24dfc1273a to your computer and use it in GitHub Desktop.
Marc Louvion's script to nuke affiliate cookie
// Nuke 'em all
// Marc's tweet https://twitter.com/marc_louvion/status/1776629697046339969
import { useEffect } from 'react';
useEffect(() => {
let referral_url = new URL(window?.location?.href);
if (
referral_url?.searchParams?.has('gclid') &&
referral_url?.searchParams?.has('via')
) {
setTimeout(function () {
document.cookie =
'rewardful.referral=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/; domain=' +
window.location.hostname;
console.log(
'Deleted referral cookie as visit came from Google Ads (forbidden by Terms of Service)',
);
}, 2000);
}
}, []);
@soulbliss
Copy link
Author

Here is the vanilla javascript approach.

<script>
  document.addEventListener("DOMContentLoaded", function() {
    // Create a new URL object based on the current window location
    let referral_url = new URL(window.location.href);

    // Check if the URL contains 'gclid' and 'via' parameters
    if (referral_url.searchParams.has('gclid') && referral_url.searchParams.has('via')) {
      // Set a timeout function to delete a specific cookie after 2 seconds
      setTimeout(function() {
        // Set the cookie to expire in the past, effectively deleting it
        document.cookie = 'rewardful.referral=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/; domain=' + window.location.hostname;
        // Log a message to the console
        console.log('Deleted referral cookie as visit came from Google Ads (forbidden by Terms of Service)');
      }, 2000);
    }
  });
</script>

Thanks @brettbatie lgtm!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment