(function(Modules) {
  "use strict";
  Modules.TabSwitcher = function() {
    this.start = function(element) {
      element.on('show.bs.tab', function(e) {
        if (window.history && window.history.replaceState) {
          window.history.replaceState(null, null, $(e.target).attr('href'));

          // Track tab switch as pageview
          // https://developers.google.com/analytics/devguides/collection/analyticsjs/pages
          if (typeof ga === "function") {
            ga('send', 'pageview');
          }
        }

        // Shim Boostrap tabs, which are only capabable of removing
        // the active class from a tab-pane that is a direct child of
        // the tab-content container. See:
        // https://github.com/twbs/bootstrap/blob/master/js/tab.js#L52
        //
        // Works in conjunction with:
        // https://github.com/alphagov/publisher/blob/master/app/assets/stylesheets/bootstrap_and_overrides.css.scss#L98-L110
        resetAllTabs();
      });

      function resetAllTabs() {
        element.find('.tab-pane').each(function() {
          $(this).removeClass('active');
        });
      }

    }
  };
})(window.GOVUKAdmin.Modules);

(function(Modules) {
  "use strict";
  Modules.TabSwitcher = function() {
    this.start = function(element) {
      element.on('show.bs.tab', function(e) {
        var tabHref = $(e.target).attr('href');

        if (window.history && window.history.replaceState) {
          window.history.replaceState(null, null, tabHref);

          // Track tab switch as pageview
          // https://developers.google.com/analytics/devguides/collection/analyticsjs/pages
          if (typeof ga === "function") {
            ga('send', 'pageview', tabHref);
          }
        }

        // Shim Boostrap tabs, which are only capabable of removing
        // the active class from a tab-pane that is a direct child of
        // the tab-content container. See:
        // https://github.com/twbs/bootstrap/blob/master/js/tab.js#L52
        //
        // Works in conjunction with:
        // https://github.com/alphagov/publisher/blob/master/app/assets/stylesheets/bootstrap_and_overrides.css.scss#L98-L110
        resetAllTabs();
      });

      function resetAllTabs() {
        element.find('.tab-pane').each(function() {
          $(this).removeClass('active');
        });
      }

    }
  };
})(window.GOVUKAdmin.Modules);

Tweak tab tracking to pass in tab href
Tweak tab tracking to pass in tab href

GA’s automatic detection of the current page wasn’t picking up the
replaced href correctly. Instead explicitly set this when sending the
page view.

https://developers.google.com/analytics/devguides/collection/analyticsjs
/pages#overriding
