GTM4WP 2.0.5 is out
GTM4WP 2.0.5 fixes “dataLayer is not defined” console errors on sites where a JavaScript delay plugin such as Flying Scripts delays scripts with gtag.
GTM4WP 2.0.5 is out on wordpress.org. It is a small bugfix release for the 2.0 line. If you use a plugin that delays JavaScript, such as Flying Scripts, and the browser console showed dataLayer is not defined errors after you updated to 2.0.3 or 2.0.4, this release fixes them. The update reaches your dashboard a few hours after the release, because wordpress.org runs an automated security review on every plugin release before it is offered as a one-click update; if you do not want to wait, you can download it from the plugin page right away. The complete list of changes is in the GTM4WP 2.0.5 changelog.
“dataLayer is not defined” with JavaScript delay plugins
Plugins like Flying Scripts speed up page loading by delaying scripts until the visitor interacts with the page. They decide which scripts to delay by matching keywords against the script code, and gtag is a common keyword on these lists. Version 2.0.3 added a line to the first script block that GTM4WP prints into the page head. That line sends the plugin’s Google tag developer ID to the data layer, and its code contained the word gtag. On a site with that keyword, the delay plugin held back the whole block. That block also declares the data layer and the plugin’s settings, so every GTM4WP script that ran after it failed with dataLayer is not defined.
From 2.0.5, the block no longer contains the word gtag, so delay plugins leave it alone, and the developer ID is still sent. If you went back to 2.0.2 or changed your keyword list as a workaround, you can update now, and your original keyword list works again without changes.
Empty data layer content
On a page where the plugin had no data layer variables to report, it pushed an empty array into the data layer. Google Tag Manager reads an array in the data layer as a command, not as data. From 2.0.5, the content is an empty object in that case and is not pushed at all, and content that is pushed is always an object.
Thank you to gemart1 for the detailed answers in the support forum, which led to the cause.
Please report problems in the support forum on wordpress.org or on GitHub.
