Expand my Community achievements bar.

SOLVED

Non-AEP Web SDK or App Measurement version of Hit Governor

Avatar

Level 1

I read the excellent post by @kenmckell on Better Bot Blocking (Part 3) and was wondering if there was a version of this implementation using Adobe Tags and the legacy App Measurement? The site we use is deployed via AEM, but does not use alloy.js / the Web SDK

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @johnanish8 ,

The hit governor function is a JS function and and can be used independently without Web SDK. The idea is to count the number of server calls sent by the device in a given time period and if that exceeds the threshold then mark the device as a bot and stop sending any more calls.

Here's how you can adapt this for your AppMeasurement implementation,

Step 1: This stays the same where you create a global rule to declare the function

  • Name: Library Loaded (Page Top) ~~ No Conditions ~~ Deploy Global Function
  • Event:
    • Extension: Core
    • Event Type: Library Loaded (Top of Page)
    • Advanced Options -> Order: 1
  • Conditions: None
  • Action:
    • Extension: Core
    • Action Type: Custom Code
    • Language: JavaScript
    • Execute Globally: Checked

To finish setting up the Action, click on the "</> Open Editor" button and copy+paste the following block of code into the code editor:

(function(){let h=globalThis.window||this;localStorage.getItem("hg_fexp")&&(new Date).getTime()>Number(localStorage.getItem("hg_fexp"))&&(localStorage.removeItem("hg_fexp"),localStorage.removeItem("hg_flag"));h.hitGovernor=function(a,c,d){a=a||60;c=c||60;d=d||60;if(localStorage.getItem("hg_flag"))localStorage.getItem("hg_flag")==="flagged"&&(a=new Date,a.setTime(a.getTime()+d*864E5),localStorage.setItem("hg_flag","1"),localStorage.setItem("hg_fexp",a.getTime().toString())),localStorage.removeItem("hg_hitt"),localStorage.removeItem("hg_hitc");else{var b=localStorage.getItem("hg_hitc")||"",e=Number(localStorage.getItem("hg_hitt"))||0;d=(new Date).getTime();b=b!==""?b.split("|").map(Number):[0,0,0,0,0];var k=b.reduce(function(f,g){return f+g},0);c=e===0?0:Math.floor((d-e)/(c/6)/1E3);e===0&&localStorage.setItem("hg_hitt",d);if(k<a) {if(c>0){if(c>5)b=[0,0,0,0,0];else for(a=0;a<c;a++)b.unshift(0),b.pop(); localStorage.setItem("hg_hitt",d)}}else localStorage.setItem("hg_flag","flagged"); b[0]++;localStorage.setItem("hg_hitc",b.join("|"));return b.reduce(function(f,g){return f+g},0)}}})();

Click on "Save" to close the editor, then click on "Keep Actions" to close the Action setup. Once you have confirmed your rule looks something like the following…

Harveer_SinghGi1_0-1750398633326.png

…click on "Save to Library".

Step 2This step will change as per AppMeasurement implementation. We don't have an sendEvent complete equivalent in AppMeasurement so what you'll have to do is add an additional action after "Adobe Analytics - Sen Beacon" action in every rule that send an Adobe Analytics tracking call, below is an example using page load rule

  • Name: AA Global Page Load ~~ No Conditions ~~ Run hitGovernor
  • Event:
    • Extension: Core
    • Event Type: Library Loaded (Top of Page)
    • Advanced Options -> Order: Greater than 1
  • Conditions: None
  • Action 1:
    • Extension: Adobe Analytics
    • Action Type: Send Beacon
    • Tracking: s.t()
  • Action 2:
    • Extension: Core
    • Action Type: Custom Code
    • Language: JavaScript
    • Execute Globally: Checked

To finish setting up the, click on the "</> Open Editor" button and copy+paste this single line of code into the code editor:

hitGovernor();

Click on "Save" to close the editor, click on "Keep Actions", then click on "Save to Library".

Step 2.5 (Optional): This also stays same, if you want to change these thresholds, you may do so by adding the following three arguments to the hitGovernor function call:

  • a (optional, integer): The hit count threshold, or the maximum number of server calls a device can send (within "b" number of seconds) before the code flags the user as a "bot".
  • b (optional, integer): The hit time threshold, or the number of seconds in the past during which the function will count the number of server calls sent.
  • c (optional, integer): The hit exclusion threshold, or the number of days a device will not be able to send out server calls after being flagged.

For example, if you want to flag devices that send out at least 100 server calls within an 80-second period and prevent them from sending any more server calls for at least 30 days, the code to deploy – refer to the second Tags rule above – will need to change to the following:

hitGovernor(100,80,30)

Be sure to carefully discuss and firmly establish the threshold settings - especially the hit exclusion threshold - with your team before deploying this code.

Step 3: There will be change in this step where the logic built in onBeforeSendEvent property of Web SDK Extension will be handled in s.doplugins section of Adobe Analytics Extension.

Once the AA Extension tracker custom code editor shows up, copy+paste the following line of code to anywhere in the s.doplugins function:

if(!!localStorage.getItem("hg_flag") && localStorage.getItem("hg_flag") === "1") s.abort = true;

The above logic checks for the existence of the localStorage flag (and the flag's value of “1”) before determining whether Adobe Analytics call should be aborted.

This is what s.doplugins function should look like:

Harveer_SinghGi1_1-1750399730958.png

Rest of the steps will stay same where you'll deploy these changes and trigger the rules.

I hope this helps.

Cheers!

View solution in original post

1 Reply

Avatar

Correct answer by
Community Advisor

Hi @johnanish8 ,

The hit governor function is a JS function and and can be used independently without Web SDK. The idea is to count the number of server calls sent by the device in a given time period and if that exceeds the threshold then mark the device as a bot and stop sending any more calls.

Here's how you can adapt this for your AppMeasurement implementation,

Step 1: This stays the same where you create a global rule to declare the function

  • Name: Library Loaded (Page Top) ~~ No Conditions ~~ Deploy Global Function
  • Event:
    • Extension: Core
    • Event Type: Library Loaded (Top of Page)
    • Advanced Options -> Order: 1
  • Conditions: None
  • Action:
    • Extension: Core
    • Action Type: Custom Code
    • Language: JavaScript
    • Execute Globally: Checked

To finish setting up the Action, click on the "</> Open Editor" button and copy+paste the following block of code into the code editor:

(function(){let h=globalThis.window||this;localStorage.getItem("hg_fexp")&&(new Date).getTime()>Number(localStorage.getItem("hg_fexp"))&&(localStorage.removeItem("hg_fexp"),localStorage.removeItem("hg_flag"));h.hitGovernor=function(a,c,d){a=a||60;c=c||60;d=d||60;if(localStorage.getItem("hg_flag"))localStorage.getItem("hg_flag")==="flagged"&&(a=new Date,a.setTime(a.getTime()+d*864E5),localStorage.setItem("hg_flag","1"),localStorage.setItem("hg_fexp",a.getTime().toString())),localStorage.removeItem("hg_hitt"),localStorage.removeItem("hg_hitc");else{var b=localStorage.getItem("hg_hitc")||"",e=Number(localStorage.getItem("hg_hitt"))||0;d=(new Date).getTime();b=b!==""?b.split("|").map(Number):[0,0,0,0,0];var k=b.reduce(function(f,g){return f+g},0);c=e===0?0:Math.floor((d-e)/(c/6)/1E3);e===0&&localStorage.setItem("hg_hitt",d);if(k<a) {if(c>0){if(c>5)b=[0,0,0,0,0];else for(a=0;a<c;a++)b.unshift(0),b.pop(); localStorage.setItem("hg_hitt",d)}}else localStorage.setItem("hg_flag","flagged"); b[0]++;localStorage.setItem("hg_hitc",b.join("|"));return b.reduce(function(f,g){return f+g},0)}}})();

Click on "Save" to close the editor, then click on "Keep Actions" to close the Action setup. Once you have confirmed your rule looks something like the following…

Harveer_SinghGi1_0-1750398633326.png

…click on "Save to Library".

Step 2This step will change as per AppMeasurement implementation. We don't have an sendEvent complete equivalent in AppMeasurement so what you'll have to do is add an additional action after "Adobe Analytics - Sen Beacon" action in every rule that send an Adobe Analytics tracking call, below is an example using page load rule

  • Name: AA Global Page Load ~~ No Conditions ~~ Run hitGovernor
  • Event:
    • Extension: Core
    • Event Type: Library Loaded (Top of Page)
    • Advanced Options -> Order: Greater than 1
  • Conditions: None
  • Action 1:
    • Extension: Adobe Analytics
    • Action Type: Send Beacon
    • Tracking: s.t()
  • Action 2:
    • Extension: Core
    • Action Type: Custom Code
    • Language: JavaScript
    • Execute Globally: Checked

To finish setting up the, click on the "</> Open Editor" button and copy+paste this single line of code into the code editor:

hitGovernor();

Click on "Save" to close the editor, click on "Keep Actions", then click on "Save to Library".

Step 2.5 (Optional): This also stays same, if you want to change these thresholds, you may do so by adding the following three arguments to the hitGovernor function call:

  • a (optional, integer): The hit count threshold, or the maximum number of server calls a device can send (within "b" number of seconds) before the code flags the user as a "bot".
  • b (optional, integer): The hit time threshold, or the number of seconds in the past during which the function will count the number of server calls sent.
  • c (optional, integer): The hit exclusion threshold, or the number of days a device will not be able to send out server calls after being flagged.

For example, if you want to flag devices that send out at least 100 server calls within an 80-second period and prevent them from sending any more server calls for at least 30 days, the code to deploy – refer to the second Tags rule above – will need to change to the following:

hitGovernor(100,80,30)

Be sure to carefully discuss and firmly establish the threshold settings - especially the hit exclusion threshold - with your team before deploying this code.

Step 3: There will be change in this step where the logic built in onBeforeSendEvent property of Web SDK Extension will be handled in s.doplugins section of Adobe Analytics Extension.

Once the AA Extension tracker custom code editor shows up, copy+paste the following line of code to anywhere in the s.doplugins function:

if(!!localStorage.getItem("hg_flag") && localStorage.getItem("hg_flag") === "1") s.abort = true;

The above logic checks for the existence of the localStorage flag (and the flag's value of “1”) before determining whether Adobe Analytics call should be aborted.

This is what s.doplugins function should look like:

Harveer_SinghGi1_1-1750399730958.png

Rest of the steps will stay same where you'll deploy these changes and trigger the rules.

I hope this helps.

Cheers!