Skip to content

Instantly share code, notes, and snippets.

@ddaws
Last active September 11, 2021 13:59
Show Gist options
  • Save ddaws/7c6738917e2f92b531b67f0b12e77dc7 to your computer and use it in GitHub Desktop.
Save ddaws/7c6738917e2f92b531b67f0b12e77dc7 to your computer and use it in GitHub Desktop.
A GTM tag to intercept all successful ajax requests on a page!
<!--
This tag pushes all ajax success events to the data layer. Ajax success events occur when an
ajax requests returns successfully, such as a ajax form submission. Ajax forms are hard to use
as triggers though this tag will expose their resulting data in the data layer to be used for triggers.
-->
<script>
var dataLayer = dataLayer || [];
$(document).ajaxSuccess(function (event, jqXHR, ajaxOptions, data) {
dataLayer.push({
event: event.type,
request: {
method: ajaxOptions.type, // GET, POST, PUT, DELETE, etc ...
url: ajaxOptions.url,
data: ajaxOptions.data,
contentType: ajaxOptions.contentType
},
response: {
url: jqXHR.responseURL,
status: jqXHR.status,
statusText: jqXHR.statusText,
data: jqXHR.response
}
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment