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.
- Go to Google Tag Manager and navigate to Variables.
- Click New, select Custom JavaScript Variable, and name it
internalTraffic
. - Insert the following code:
function() {
return localStorage.getItem("internalTraffic") === "true" ? "true" : "false";
}
- 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.
- Use the Custom Variable:
- Ensure the
internalTraffic
custom JavaScript variable is set up as described in Step 1.
- Ensure the
- Set Up a Trigger:
- Go to Triggers > New.
- Choose Page View as the trigger type.
- Select Some Page Views.
- Define the condition:
{{internalTraffic}}
equalstrue
. - 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.
- Go to Tags > New and create a new Google Analytics: GA4 Event tag.
- Set the event name to
internal_traffic
. - Under Event Parameters, add a parameter:
- Parameter Name:
internal_traffic
- Value:
true
- Parameter Name:
- Set the trigger to the Internal Traffic Trigger created earlier.
- 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.
- Open Google Analytics and go to Admin > Data Settings > Data Filters.
- Click Create New Filter and name it Internal Traffic.
- Select Custom Parameter and set it to
internal_traffic
. - Choose Exclude Traffic to prevent internal visits from affecting your data.
- 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.

Alex is an experienced SEO consultant with over 14 years of working with global brands like Montblanc, Ricoh, Rogue, Gropius Bau and Spartoo. With a focus on data-driven strategies, Alex helps businesses grow their online presence and optimise SEO efforts.
After working in-house as Head of SEO at Spreadshirt, he now works independently, supporting clients globally with a focus on digital transformation through SEO.
He holds an MBA and has completed a Data Science certification, bringing strong analytical skills to SEO. With experience in web development and Scrum methodologies, they excel at collaborating with cross-functional teams to implement scalable digital strategies.
Outside work, he loves sport: running, tennis and swimming in particular!