How to Track Internal Traffic While Managing Dynamic IP Addresses – with JavaScript and GTM

Analytics

Using a JavaScript button with GTM to identify internal users is a flexible and effective way to filter internal traffic without relying on IP addresses. This method is particularly useful for remote teams and mobile users who frequently change networks. By implementing this approach, you can ensure cleaner and more accurate website analytics, leading to better data-driven decisions.

Tracking internal traffic separately from external users is crucial for accurate website analytics. If employees or team members frequently visit your site, their activity can distort key metrics like bounce rate, conversions, and engagement. Google Analytics provides a way to filter internal traffic, but sometimes, a more flexible and dynamic solution is needed—one that doesn’t rely solely on IP filtering. One effective approach is using a JavaScript button and Google Tag Manager (GTM) to mark internal users.

A JavaScript button allows users to self-identify as internal traffic, making it useful when employees work remotely, use different networks, or access your site on mobile devices where IP filtering isn’t reliable. This method enables users to opt-in as internal traffic by clicking a button, which sets a persistent identifier in local storage or cookies.

Step 1: Create a Custom Variable in GTM

First, you need to set up a custom variable in GTM to store the internal traffic status. This variable will be used later to trigger tags or define user segments in Google Analytics.

  1. Go to Google Tag Manager and navigate to Variables.
  2. Click New, select Custom JavaScript Variable, and name it internalTraffic.
  3. Insert the following code:
function() {
  return localStorage.getItem("internalTraffic") === "true" ? "true" : "false";
}
  1. Save the variable.

Step 2: Add JavaScript Button to Your Bookmarks / Favourites

Next, add a simple button to your bookmarks that, when clicked, will mark a user as internal traffic by storing a value in localStorage.

<button id="markInternal">Mark as Internal User</button>
<script>
document.getElementById("markInternal").addEventListener("click", function() {
    localStorage.setItem("internalTraffic", "true");
    alert("You are now marked as internal traffic.");
});
</script>

This script stores the internal traffic status in localStorage, ensuring that the preference is remembered across sessions.

Step 3: Capture the Internal Traffic Status in GTM

Now, configure GTM to read the internal traffic status and pass it to Google Analytics.

  1. Use the Custom Variable:
    • Ensure the internalTraffic custom JavaScript variable is set up as described in Step 1.
  2. Set Up a Trigger:
    • Go to Triggers > New.
    • Choose Page View as the trigger type.
    • Select Some Page Views.
    • Define the condition: {{internalTraffic}} equals true.
    • Save the trigger.

Step 4: Send Internal Traffic Data to Google Analytics

Now that GTM can identify internal users, create a tag to send this data to Google Analytics.

  1. Go to Tags > New and create a new Google Analytics: GA4 Event tag.
  2. Set the event name to internal_traffic.
  3. Under Event Parameters, add a parameter:
    • Parameter Name: internal_traffic
    • Value: true
  4. Set the trigger to the Internal Traffic Trigger created earlier.
  5. Save and publish your changes in GTM.

Step 5: Create an Internal Traffic Segment in Google Analytics

Once internal traffic data is being sent to GA4, you can create a segment to filter it out of reports.

  1. Open Google Analytics and go to Admin > Data Settings > Data Filters.
  2. Click Create New Filter and name it Internal Traffic.
  3. Select Custom Parameter and set it to internal_traffic.
  4. Choose Exclude Traffic to prevent internal visits from affecting your data.
  5. Save and apply the filter.

Testing and Validation

To ensure everything is working correctly:

  • Use Preview Mode in GTM to check that the internalTraffic event fires when the button is clicked.
  • In Google Analytics, navigate to DebugView and verify that the internal_traffic parameter is received.
  • Visit your website as an external user and confirm that no internal traffic data is mistakenly recorded.