• Resolved Against The Odds

    (@againsttheodds)


    Hi, I have tried to use this filter, but with no success. This simple test code just doesn’t push anything to the DataLayer:

    add_filter('gtm4wp_compile_datalayer', function($dataLayer){
    $dataLayer['language_website'] = "ES";
    return $dataLayer;
    });

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Thomas Geiger

    (@duracelltomi)

    Hi @againsttheodds,

    Thank you for the report. The filter name and the callback in your code are both correct, so this should work. There are a few things around it that can prevent it from running.

    The data layer is only compiled if a GTM container ID is saved in the plugin settings. If that field is empty, for example because you add the container code to your site in some other way, the filter is never called and nothing is added to the data layer. Please check this first.

    The filter runs during wp_head, so your code needs to be registered before that. Adding it to the functions.php file of your theme or to a small plugin is fine, but adding it on a later hook is not.

    It is also worth checking where you are looking for the value. The plugin merges the result of the filter into the first data layer push that it writes into the page head, so your key is inside the dataLayer_content variable in the page source and not in a separate push later on. The quickest test is to view the page source and search for language_website.

    If you find dataLayer_content there with the variables of the plugin but without your own key, then the filter is not registered early enough. If dataLayer_content is missing completely, then the container ID is what you need to check. Please also clear your page cache before you test, otherwise you may be looking at an older copy of the page.

    Please let me know what you find so that I can help you further.

    Thread Starter Against The Odds

    (@againsttheodds)

    Hi Thomas!

    A GTM container ID is saved in the plugin settings, with no other container added elsewhere.

    The issue persists even if only your plugin and the code snippet with the filters are enabled in WordPress.

    I’m looking for the value in the first data layer push, the same one where all the geolocation information from IPstack is pushed to. It’s just not there, nor anywhere else, including the page source, as you suggested.

    All cache levels are deactivated: object cache, page cache, cloudflare cache and browser cache.

    The container ID is correctly set up in your plugin, and my GTM is correctly receiving the rest of the data your plugin pushes to the DataLayer.

    I’ve been using your plugin for years, and everything works fine globally. It’s just this filter that doesn’t work.

    Plugin Author Thomas Geiger

    (@duracelltomi)

    Hi @againsttheodds,

    Thank you, that narrows it down a lot. The geolocation values you already see in that first push are added by the plugin through the same gtm4wp_compile_datalayer filter that your code uses, so the filter itself is running on your site. The only thing missing is your own callback, which means it is most likely not registered yet at the moment the plugin builds that push in the page head.

    The usual reason for this is where the snippet lives. If you add it through a code snippet plugin, please check that the snippet is set to run on the front end and early, not only in the admin area and not on a late hook, because some of these tools run the code later than the theme would, and by then the head has already been written. As a direct test, could you please move the exact same code into the functions.php file of your active theme for a moment and load a page again? A filter registered from functions.php is always in place before the head is built, so if language_website shows up that way, the timing of the snippet was the cause.

    If the value is still missing even from functions.php, then something more unusual is going on and I would like to look into it further. In that case please let me know your GTM4WP version and whether the “Load GTM container as early as possible” option is turned on, so that I can try to reproduce it on my side.

    Thread Starter Against The Odds

    (@againsttheodds)

    Okay, I played around with the Code Snippets plugin insertion settings and fixed the issue. Thanks a lot for your help!

Viewing 4 replies - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.