Set parameter for internal or external traffic based on IP We want to block external traffic in Google Tag Manager and I am trying to do something like shown here.

I created this:

/**
 * Implements hook_init().
 */
function myfunction_init() {

  $internal_ips = [
    'x.x.x.x',
    'x.x.x.x',
  ];

  $internal_traffic = 'no';
  if (in_array(ip_address(), $internal_ips)) {
    $internal_traffic = 'yes';
  }

  $data_layer = [
    '#type' =&gt; 'markup',
    '#tag' =&gt; 'script',
    '#markup' =&gt; "&lt;script&gt;
                    var dataLayer = [];
                    dataLayer.push({ 'internalTraffic': '$internal_traffic'});
                  &lt;/script&gt;",
    '#weight' =&gt; 100 ,
  ];

  drupal_add_html_head($data_layer, 'data-layer');
}


It would work but the problem is I have cached pages so this will not be correct.  I did try hook_boot but that did not work because drupal_add_html_head() is not a valid function (I'm assuming it wasn't initialized yet).
To request the uncached client IP I recommend providing a JSON endpoint that has   drupal_page_is_cacheable(FALSE) set. Then get the IP from there with an Ajax request and do whatever you need to do now with JavaScript.

First, provide a route.

/**
 * Implements hook_menu().
 */
function MYMODULE_menu() {

  $items['myip'] = [
    'page callback'   =&gt; '_MYMODULE_myip_callback',
    'access callback' =&gt; TRUE,
  ];

  return $items;
}


Second, provide the endpoint that returns the result of the IP comparison. (Bonus points for getting the internal IPs from an admin settings form. More bonus points for checking the user role as well.)

/**
 * Callback to provide an uncached JSON response.
 */
function _MYMODULE_myip_callback() {

  $internal_ips = [
    'x.x.x.x',
    'x.x.x.x',
  ];

  $data = [
    'internal_ip' =&gt; in_array(ip_address(), $internal_ips) ? TRUE : FALSE,
  ];

  // Mark this page as being uncacheable.
  drupal_page_is_cacheable(FALSE);

  drupal_json_output($data);
}


Third, add the JS.

/**
 * Implements hook_page_build().
 */
function MYMODULE_page_build(&amp;$page) {

  drupal_add_js(drupal_get_path('module', 'MYMODULE') . '/js/scripts.js', ['scope' =&gt; 'footer']);
}


Fourth, the Ajax request and DOM manipulation in the JS file.

(function ($, Drupal) {
  'use strict';
  Drupal.behaviors.myBehavior = {
    attach: function (context, settings) {

      $.getJSON('/myip', function (result) {

        if (result.internal_ip) {
          function_to_call_if_internal_ip();
        }
      });

      var function_to_call_if_internal_ip = function () {

        // Do whatever you need to do.
        // ...

      };

    }
  };
})(jQuery, Drupal);




Note

One thing to mention. I adjusted the sample to follow your sample flow "do something if internal IP". In the original snippet I do the opposite "only add Google Tag Manager if external IP" to never have the tag manager script added at all if the request comes from an internal IP and it works just fine.

$.getJSON('/myip', function (result) {

  if (result.add_scripts_by_client_ip &amp;&amp; result.add_scripts_by_user &amp;&amp; result.add_scripts_by_host) {
    _add_google_tag_manager('GTM-XXXXX');
  }
});

var _add_google_tag_manager = function (gtmid) {

  if (!!gtmid &amp;&amp; gtmid !== null &amp;&amp; gtmid !== '') {

    $('html').once('myBehavior').each(function () {
      $('body').prepend('&lt;noscript&gt;&lt;iframe src="https://www.googletagmanager.com/ns.html?id=' + gtmid + '" height="0" width="0" style="display:none;visibility:hidden"&gt;&lt;/iframe&gt;&lt;/noscript&gt;');
      $('head').append("&lt;script&gt;(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],j=d.createElement(s),dl=l!='dataLayer'?'&amp;l='+l:'';j.async=true;j.src='https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);})(window,document,'script','dataLayer', '" + gtmid + "');&lt;/script&gt;");
    });
  }
};




If you use Varnish, Memchached or some other server-side caching strategy, don't forget to exclude /myip from them.