When setting up Google Analytics 4 (GA4) events, a common task is to fire an event when a user clicks a button. Two popular methods for accomplishing this on the client side are using inline JavaScript directly in the HTML or by attaching an event listener via a separate <script> block.
A key question for developers is: which method is more secure against client-side manipulation? The answer is that while neither is foolproof, they differ in how easily a user with malicious intent can tamper with the data sent to GA4.
Method 1: Inline JavaScript
This approach involves placing the JavaScript code directly within an HTML attribute, most commonly “onclick.” This makes the code self-contained and easy to read in the HTML source.
<button id= "copyright" onclick="dataLayer.push({event: "copyright",shape: "round"});">Send</button>
Client-Side Tampering with Inline JS
This method is the most straightforward to manipulate using browser developer tools. A user can open the "Elements" panel in DevTools, find the button element, and directly edit the “onclick” attribute. For example, they could change the shape value from "round" to "oval".
When the user clicks the button, the browser will fire the updated JavaScript code from the modified attribute, and the dataLayer will receive the manipulated value.
This is because the browser's JavaScript engine executes the code as it exists in the DOM at the time of the click. The change is immediate and simple to perform.
Method 2: Embedded or External JavaScript
This method separates the HTML structure from the event-handling logic. A JavaScript function is used to find the button element by its ID and attach a click event listener to it.
This script can be embedded directly in a <script> tag on the page or linked as an external .js file:
<button id="copyright">Send</button>
<script>
document.getElementById('copyright').addEventListener('click', function() {
dataLayer.push({
event: "copyright",
shape: "round"
});
});
</script>
Client-Side Tampering with Event Listeners
While this method is more robust than inline JavaScript, it is by no means completely secure. Manipulating the event data is still possible, but it requires a deeper understanding of how the browser's JavaScript engine works.
The key difference is that the JavaScript code that registers the event listener is not part of the DOM representation shown in the "Elements" panel. Therefore, if you inspect the <script> tag in the DevTools "Elements" panel and try to change the value of shape from "round" to "oval" there, nothing will happen. When you click the button, the value sent to the dataLayer will still be "round".
Here's why:
-
DevTools Elements Panel vs. Browser's JavaScript Engine: The "Elements" panel shows you a representation of the current Document Object Model (DOM). You can edit the HTML and attributes in this panel, and you'll see those changes on the page. However, this panel is not a live editor for the JavaScript code that has already been loaded and parsed by the browser.
-
Static Code vs. Live Execution: The <script> tag contains a static block of JavaScript code. Once the browser has parsed and executed that code to set up the event listener, the code is no longer "live" in a way that the "Elements" panel can edit. The event listener is already registered with the original function, which contains shape: "round".
To tamper with the event listener, a user would need to use the "Console" panel to execute new JavaScript code. This process is more complex, typically involving removing the old event listener (for example, by cloning the button element) and then creating a new one with the desired changes.
Conclusion
Neither inline JavaScript nor event listeners are 100% secure from client-side manipulation. A determined user can always use browser developer tools to intercept and modify the data being sent to GA4. However, using an embedded or external JavaScript file to register an event listener is slightly better because it takes more time and technical knowledge to tamper with the values. Simple edits in the DevTools "Elements" panel won't work, forcing the user to interact with the JavaScript engine through the "Console" panel.
For events that are tied to highly sensitive data, such as a purchase amount or user ID, it is always best to use server-side tracking. By sending event data directly from your server to GA4, you eliminate the possibility of client-side tampering, making the data you collect significantly more reliable and secure.