Google Tag Manager - Alternative "Referrer Url" Method

A guide to using an alternative method for event tracking on embedded NBI Widget

Note: This method is discouraged and in most cases you should try the Advance setup instead.

Sometimes hardcoding the referrer URL's value is not plausible (as explained on Basic "referrer url" method for event tracking on embedded NBI Widget ), because the URL may contain unknown 'dynamic' values.

If you are not able to dynamically set the#referrer=MY_REFERRER_URLfragment on page load using a Back End language (such as PHP, C#, Java, etc), which may be the case if you're using a website builder like Wix or Squarespace, you can still set it using the JavaScript (after the iFrame's HTML), like this:

 <script>
var iframe = document.querySelector('[data-id="nbi-widget"]'); iframe.src += '#referrer=' + window.location.href;
</script>

In some cases, such as with Squarespace, the code entry areas are actually already inside an iFrame, so you need to get the URL from another level up the iframe/parent ladder, by usingwindow.parentand using that instead, like this:

 <script>
var iframe = document.querySelector('[data-id="nbi-widget"]'); iframe.src += '#referrer=' + window.parent.location.href;
</script>

If you need to use a tracking ID, first get the tracking id from your page's URL like this, swapping 'mytrackingid' with the parameter name:

 <script>
var url = new URL(window.location.href); var trackingId = url.searchParams.get("mytrackingid");
</script>

Then use the 'id' in the widget's iFrame 'src' like this:

<script>
var iframe = document.querySelector('[data-id="nbi-widget"]'); iframe.src += '#referrer=' + trackingId;
</script>