Data Layer
Menu The Data Layer
 

The Data Layer - Google Global Tag Manager

Looking at the Google tracking code for global site tag I was curious about the syntax ||.

|| in Javascript is a Logical OR.

What Google Developers webite says

" A data layer is a JavaScript object that is used to pass information from your website to your Tag Manager container. You can then use that information to populate variables and activate triggers in your tag configurations. ... Transaction data: Cart value, checkout date. Customer information: New or returning customer. "

Top

The logical OR (||) (logical disjunction) operator

" The logical OR (||) (logical disjunction) operator for a set of operands is true if and only if one or more of its operands is true. It is typically used with boolean (logical) values. When it is, it returns a Boolean value. However, the || operator actually returns the value of one of the specified operands, so if this operator is used with non-Boolean values, it will return a non-Boolean value. "

Looking at the Google tracking code:

The gtag() script:

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-936952-6"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', 'UA-936952-6');
</script>

The logical OR - || - instatiates the JavaScript Object dataLayer if the array is empty.

The object is populated by the gtag() function.

There is a lot going on with the gtag()

Links

Top

References: - a note on these

  • The Data Layer - Tag Manager Help - https:// support.google.com/tagmanager/ answer/6164391?hl=en
  • Logical OR (||) - https:// developer.mozilla.org/en-US/docs/ Web/JavaScript/Reference/ Operators/Logical_OR
  • What is a data layer? - https://segment.com/blog/what-is-a-data-layer/ - An introduction to the data layer: what it is, how it works, and how it can benefit your business.
  • Vertical_bar (|) - https:// en.wikipedia.org/wiki/ Vertical_bar#Solid_vertical _bar_vs_broken_bar

Site design by Tempusfugit Web Design -