Solved

Possible to block/filter user agent from all data?

  • 12 September 2023
  • 1 reply
  • 102 views

Badge

I saw that it is possible to block traffic from specific user agents from Session Replay. Would it also be possible to block all data collection from a list of specified user agents the way that we can for IP addresses? Or to filter based on user agent?

My use case is that we have an automated testing suite that we run against our site. Since it can be executed from our CI/CD pipeline (in the cloud), excluding by IP won’t work for us. However, I can set up the test framework to send a specific user agent.

icon

Best answer by ALabs I Bhupender 13 September 2023, 10:24

View original

1 reply

Userlevel 3
Badge +1

Hi,

Heap right now does not provide the feature in the UI to stop Heap from capturing data for sessions based on User Agent, as Heap could not know the user agent on your platform before running the script, hence, it would have to load (start capturing the session) to know the user agent.

However, you can block Heap to capture data for these testing suite sessions by adding a logic to your Heap snippet code and selectively loading Heap.

I have provided a sample code below that can block Heap from capturing data for specific user agent.

Script to define Heap:

<script type="text/javascript">   
window.heap=window.heap||[],heap.load=function(e,t){window.heap.appid=e,window.heap.config=t=t||{};var r=document.createElement("script");r.type="text/javascript",r.async=!0,r.src="https://cdn.heapanalytics.com/js/heap-"+e+".js";var a=document.getElementsByTagName("script")[0];a.parentNode.insertBefore(r,a);for(var n=function(e){return function(){heap.push([e].concat(Array.prototype.slice.call(arguments,0)))}},p=["addEventProperties","addUserProperties","clearEventProperties","identify","resetIdentity","removeEventProperty","setEventProperties","track","unsetEventProperty"],o=0;o<p.length;o++)heap[p[o]]=n(p[o])};
</script>

Script to selectivelly loading Heap:

    <script>
// Get the user agent string from the browser
var userAgent = navigator.userAgent;

// Strings we want to exclude (replace with the strings for your case)
var blockedStrings = ["testing_suite1", "testing_suite2"];

// Validating the user agent
function isUserAgentNotBlocked(userAgent) {
for (var i = 0; i < blockedStrings.length; i++) {
if (userAgent.includes(blockedStrings[i])) {
return false;
}
}
return true;
}

// Running Heap only if user agent is not to be blocked
if (isUserAgentNotBlocked(userAgent)) {
// User agent does not contain blocked strings, run Heap
heap.load("YOUR_APP_ID");
} else {
// User agent contains blocked strings, do not run Heap
console.log("Heap not loaded for the user agent");
}
</script>

Hope this helps!

Reply