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
,i
only being used as variable declaration, andd
ande
are unused other than making the original parameter listasync hide
.document.documentElement
represents the<html>
element.className += ' '
normalizes our variable to aString
and is setting theclass
attribute on `+ 'async-hide';
sets the class toasync-hide
which will be used later.h.start = 1 * new Date();
sets on our options Object a time 1 second from nowh.end = i = function ()
sets theend
property and assigns ouri
parameter a valuedocument.documentElement.className = document.documentElement.className.replace(RegExp(' ?' + 'async-hide'), '')
really convoluted way to remove only theasync-hide
class from<html>
(window['dataLayer'] = window['dataLayer'] || [])
grabs thedataLayer
list from Google Tag Manager or returns an empty Array.hide = h;
sets thehide
property to out options objecti();
calls our function to remove the classh.end = null
removes 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
asynchide
though!Here's my stab at it without running the code.
So the style is to set an element to
opacity: 0
no matter what because of the!important
. It does this on the root element of the documentdocument.documentElement
by appending the CSS classasync-hide
vias.className+=' '+y
, wheres
is `document.documentElement.Then it adds a property
start
who's value is1*newDate
to the object being passed in{'GTM-PNDQRRW':true}
which is represented by the parameterh
in the anonymous immediately invoked function. It also sets anend
property 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]
, wheren
is'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 ahide
property which is equal to the objecth
, the enhanced{'GTM-PNDQRRW':true}
, object.From there a
setTimeout
is invoked which fires after4000
milliseconds (taken from the parameterc
. Inside the timeout, it runs the functioni
which is the same reference as the functionh.end
. Oncei()
has run, it's set tonull
viah.end = null
. While the timeout is set to run in 4 seconds, the enhanced object represented byh
gets atimeout
property whose value isc
(4000ms).I'm assuming because of
GTM
in 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.documentElement
and 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!