Disable outbound link tagging on a specific link

Is there any way to disable outbound link tagging on a specific link.

The query makes no sense in mailto: anchors and breaks tel: anchors.

I appreciate any suggestions/help

/Emil

1 Like

Hello, @Maddemacher! Did you discover the solution? I found a link that breaks because of the outbound link tagging, but do not want to disable all.

One option would be to remove the automatic tagging done by Ghost (with the slider bar in the analytics section), and put your own code into code injection (or the theme). That’ll give you fuller control over what it does.

(Is it still tagging mailto and tel, or did that get fixed? If it is, would you please open a bug over on Github?)

Here’s a quick example:

// Define your domain to exclude and the specific link to ignore
const myDomain = "mydomain.com";
const mySpecialLink = "https://example.com/special";
const sitename = "your-href-text";

// Loop through all anchor tags on the page
document.querySelectorAll('a').forEach(anchor => {
    const url = new URL(anchor.href);
    
    // Check if the URL starts with http or https, is not on your domain, is not the special link, and doesn't already have "ref"
    if ((url.protocol === 'http:' || url.protocol === 'https:') &&
        url.hostname !== myDomain &&
        anchor.href !== mySpecialLink &&
        !url.searchParams.has('ref')) {
        
        // Add or append "ref=sitename" to the URL
        url.searchParams.append('ref', sitename);
        
        // Update the href with the modified URL
        anchor.href = url.toString();
    }
});
1 Like

Thank you for the inspiration! I didn’t go fully through the way you described but recalled that there is an opportunity to do code injections on the page.

So I pasted the following code into my footer (so has the full page already):

<script type="module">
  const domainToDeref = "<Paste address here, should start exactly as the link on the page. E.g., to remove ?ref=<...> from https://example.com/?ref=<...> put here https://example.com/>";
  const currentDomain = location.hostname.replace("www.", "");

  Array.from(document.querySelectorAll("a"))
    .filter((link) => link.href.startsWith(domainToDeref))
    .forEach(
      (link) => (link.href = link.href.replace(`?ref=${currentDomain}`, "")),
    );
</script>
1 Like