Advertisement
JITreviso

ajaxComplete - form

Jun 6th, 2022
1,092
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script id="gtm-jq-ajax-listen" type="text/javascript">
  2.  (function() {
  3.  
  4.  'use strict';
  5.  var $;
  6.  var n = 0;
  7.  init();
  8.  
  9.  function init(n) {
  10.  
  11.  // Ensure jQuery is available before anything
  12.  if (typeof jQuery !== 'undefined') {
  13.  
  14.  // Define our $ shortcut locally
  15.  $ = jQuery;
  16.  bindToAjax();
  17.  
  18.  // Check for up to 10 seconds
  19.  } else if (n < 20) {
  20.  
  21.  n++;
  22.  setTimeout(init, 500);
  23.  
  24.  }
  25.  
  26.  }
  27.  
  28.  function bindToAjax() {
  29.  
  30.  $(document).bind('ajaxComplete', function(evt, jqXhr, opts) {
  31.  
  32.  // Create a fake a element for magically simple URL parsing
  33.  var fullUrl = document.createElement('a');
  34.  fullUrl.href = opts.url;
  35.  
  36.  // IE9+ strips the leading slash from a.pathname because who wants to get home on time Friday anyways
  37.  var pathname = fullUrl.pathname[0] === '/' ? fullUrl.pathname : '/' + fullUrl.pathname;
  38.  // Manually remove the leading question mark, if there is one
  39.  var queryString = fullUrl.search[0] === '?' ? fullUrl.search.slice(1) : fullUrl.search;
  40.  // Turn our params and headers into objects for easier reference
  41.  var queryParameters = objMap(queryString, '&', '=', true);
  42.  var headers = objMap(jqXhr.getAllResponseHeaders(), '\n', ':');
  43.  
  44.  // Blindly push to the dataLayer because this fires within GTM
  45.  dataLayer.push({
  46.  'event': 'ajaxComplete',
  47.  'attributes': {
  48.  // Return empty strings to prevent accidental inheritance of old data
  49.  'type': opts.type || '',
  50.  'url': fullUrl.href || '',
  51.  'queryParameters': queryParameters,
  52.  'pathname': pathname || '',
  53.  'hostname': fullUrl.hostname || '',
  54.  'protocol': fullUrl.protocol || '',
  55.  'fragment': fullUrl.hash || '',
  56.  'statusCode': jqXhr.status || '',
  57.  'statusText': jqXhr.statusText || '',
  58.  'headers': headers,
  59.  'timestamp': evt.timeStamp || '',
  60.  'contentType': opts.contentType || '',
  61.  // Defer to jQuery's handling of the response
  62.  'response': (jqXhr.responseJSON || jqXhr.responseXML || jqXhr.responseText || '')
  63.  }
  64.  });
  65.  
  66.  });
  67.  
  68.  }
  69.  
  70.  function objMap(data, delim, spl, decode) {
  71.  
  72.  var obj = {};
  73.  
  74.  // If one of our parameters is missing, return an empty object
  75.  if (!data || !delim || !spl) {
  76.  
  77.  return {};
  78.  
  79.  }
  80.  
  81.  var arr = data.split(delim);
  82.  var i;
  83.  
  84.  if (arr) {
  85.  
  86.  for (i = 0; i < arr.length; i++) {
  87.  
  88.  // If the decode flag is present, URL decode the set
  89.  var item = decode ? decodeURIComponent(arr[i]) : arr[i];
  90.  var pair = item.split(spl);
  91.  
  92.  var key = trim_(pair[0]);
  93.  var value = trim_(pair[1]);
  94.  
  95.  if (key && value) {
  96.  
  97.  obj[key] = value;
  98.  
  99.  }
  100.  
  101.  }
  102.  
  103.  }
  104.  
  105.  return obj;
  106.  
  107.  }
  108.  
  109.  // Basic .trim() polyfill
  110.  function trim_(str) {
  111.  
  112.  if (str) {
  113.  
  114.  return str.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  115.  
  116.  }
  117.  
  118.  }
  119.  
  120.  
  121.  })();
  122.  /*
  123.  * v0.1.0
  124.  * Created by the Google Analytics consultants at http://www.lunametrics.com
  125.  * Written by @notdanwilkerson
  126.  * Documentation: http://www.lunametrics.com/blog/2015/08/27/ajax-event-listener-google-tag-manager/
  127.  * Licensed under the Creative Commons 4.0 Attribution Public License
  128.  */
  129. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement