Updating a variable in dataLayer Google Tag Manager

If need updating a variable in dataLayer use code:

<script>
  window.google_tag_manager["GTM-XXXXXXX"].dataLayer.set("variable", "value");
</script>

Where “GTM-XXXXXXX” — container id or use “{{Container ID}}” (automatically set our container id), “variable” — variable for updating, “value” — new value. Example work code:

<script>
  window.google_tag_manager[{{Container ID}}].dataLayer.set("userName", "Admin");
</script>