In trying to understand a Google snippet I had the thought that it might be an interesting type of discussion—explaining a code snippet.
This code is minimized for efficiency. Anyone care to read this to me?
<style>.async-hide { opacity: 0 !important} </style>
<script>(function(a,s,y,n,c,h,i,d,e){s.className+=' '+y;h.start=1*new Date;
h.end=i=function(){s.className=s.className.replace(RegExp(' ?'+y),'')};
(a[n]=a[n]||[]).hide=h;setTimeout(function(){i();h.end=null},c);h.timeout=c;
})(window,document.documentElement,'async-hide','dataLayer',4000,
{'GTM-PNDQRRW':true});</script>
Top comments (7)
TLDR: What this snippet does is assign a class, namely
'async-hide'to the<html>element of our document, waits 4 seconds, and then removes it.We have a self running function with a bunch of parameters but let's "prettify" it first.
Hmmm. That didn't help much. Okay. Let's replace all the parameters with their actual variables.
Much better. Now we have left the options object being passed as
h,ionly being used as variable declaration, anddandeare unused other than making the original parameter listasync hide.document.documentElementrepresents the<html>element.className += ' 'normalizes our variable to aStringand is setting theclassattribute on `+ 'async-hide';sets the class toasync-hidewhich will be used later.h.start = 1 * new Date();sets on our options Object a time 1 second from nowh.end = i = function ()sets theendproperty and assigns ouriparameter a valuedocument.documentElement.className = document.documentElement.className.replace(RegExp(' ?' + 'async-hide'), '')really convoluted way to remove only theasync-hideclass from<html>(window['dataLayer'] = window['dataLayer'] || [])grabs thedataLayerlist from Google Tag Manager or returns an empty Array.hide = h;sets thehideproperty to out options objecti();calls our function to remove the classh.end = nullremoves the reference to our function from the options objecth.timeout = 4000;the snippet waits 4 seconds before doing anythingSo.
What this snippet does is assign a class, namely
'async-hide'to the<html>element of our document, waits 4 seconds, and then removes it.Addendum, and I almost forgot! The CSS makes the entire document invisible. Presumably to wait for the page to load and hoping everything is done by then so as to not get a FOUC or similar.
1*new Date()turns the date object into an integer.I tried to un-minimize it. Not all the way there, but at least now it's easier to see what the code is doing. 😁
Totally cheated, but this explainer for it is pretty good. It hides the page content until the Optimize container is loaded in. Hilarious that they added those extra parameters in to write out
asynchidethough!Here's my stab at it without running the code.
So the style is to set an element to
opacity: 0no matter what because of the!important. It does this on the root element of the documentdocument.documentElementby appending the CSS classasync-hidevias.className+=' '+y, wheresis `document.documentElement.Then it adds a property
startwho's value is1*newDateto the object being passed in{'GTM-PNDQRRW':true}which is represented by the parameterhin the anonymous immediately invoked function. It also sets anendproperty on the same object whose value is a functionfunction(){s.className=s.className.replace(RegExp(' ?'+y),'')};After that it sets a global var on the window, parameter
a, viaa[n], wherenis'dataLayer'. It sets itself to itself if it exists, otherwise it's initialized to an empty array,(a[n]=a[n]||[]). Onwindow.dataLayer(froma[n]), it sets ahideproperty which is equal to the objecth, the enhanced{'GTM-PNDQRRW':true}, object.From there a
setTimeoutis invoked which fires after4000milliseconds (taken from the parameterc. Inside the timeout, it runs the functioniwhich is the same reference as the functionh.end. Oncei()has run, it's set tonullviah.end = null. While the timeout is set to run in 4 seconds, the enhanced object represented byhgets atimeoutproperty whose value isc(4000ms).I'm assuming because of
GTMin the object key that this is some Google Tag Manager voodoo?So all in all what does this do? It looks like it hides
document.documentElementand then removes the CSS class that had it's opacity set to 0 for 4 seconds and this action seems to be related to this'GTM-PNDQRRW'.a,s,y,n,c,h,i,d,e
The function doesn't use d or e variables. Somebody is just being cute.
Looks like it assigns a class name to an element for 4 seconds!