Why is there “pegasaas-lighthouse-ua-opt-out” code in the head of my page?

Why is there “pegasaas-lighthouse-ua-opt-out” code in the head of my page?

You are here:
  • KB Home
  • FAQs
  • Why is there "pegasaas-lighthouse-ua-opt-out" code in the head of my page?
< Back

This code is in the head of your page to prevent any lighthouse/pagespeed scans of your page from being recorded in Google Analytics. If this code were not in your page, you would have a number of irrelevant hits recorded from the Google Lighthouse / PageSpeed system, artificially inflating your bounce rate. Let’s break down the code.

Example Code

<script id='pegasaas-lighthouse-ua-opt-out'>
if (navigator && navigator.userAgent && navigator.userAgent.search('Chrome-Lighthouse') > 0) { console.log('Opting Out of Analytics'); window['_gaUserPrefs'] = { ioo : function() { return true; } }; window['ga-disable-UA-XXXXXXX-1'] = true;

The Conditional

if (navigator && navigator.userAgent && navigator.userAgent.search('Chrome-Lighthouse') > 0) { What this is saying is that if the web browser’s user agent has the string “Chrome-Lighthouse”, which the Lighthouse system uses when it scans your page for web performance metrics, then do the following code.

Inside The Conditional

So, if we’ve passed the test, then three lines of code execute. console.log('Opting Out of Analytics'); This line simply will appear in the web browser’s console output.  If you don’t see this output, then the system is not opting out of analytics. window['_gaUserPrefs'] = { ioo : function() { return true; } }; This line informs Google Analytics that this page hit is to opt out of analytics. window['ga-disable-UA-XXXXXXX-1'] = true; This is a secondary mechanism to disable Google Analytics for this page hit.

In Conclusion

This code is injected into your page to prevent irrelevant hits by the web performance scanning system.  It will not interfere with legitimate hits to your web page.

Comments are closed.