• I would like to add custom variables to the dataLayer. How can I do this?

    For example, I want to pull the country code from the current page URL and send it as a “country” variable. Is this possible?

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

    (@duracelltomi)

    Hi,

    Thanks for your question!

    There is a constant defined with my plugin:
    GTM4WP_WPFILTER_COMPILE_DATALAYER

    You may use this to add a WordPress filter hook and add whatever you want to the data layer:

    function my_dl_content( $dl ) {
      $dl[ "myData" ] = "Something";
    
      return $dl
    }
    add_filter( GTM4WP_WPFILTER_COMPILE_DATALAYER, "my_dl_content" );

    However in your specific case I think there is no need to add such code to your site: you may simply create a Custom JavaScript Variable on the GTM UI where you can use a simple JS code to extract the country code:

    function() {
      var pu = {{Page URL}},
          pu_parts = pu.split( "." );
    
      if ( pu_parts.length > 2 ) {
        return pu_parts[0]; // return first subdomain
      } else {
        return "en"; // fallback default value
      }
    }
Viewing 1 replies (of 1 total)
  • The topic ‘How to Add Custom Variables’ is closed to new replies.