Skip to content

Instantly share code, notes, and snippets.

@vyspiansky
Last active October 6, 2025 12:50
Show Gist options
  • Select an option

  • Save vyspiansky/5019feaee88dc2defd09bbbb7c8b2cfc to your computer and use it in GitHub Desktop.

Select an option

Save vyspiansky/5019feaee88dc2defd09bbbb7c8b2cfc to your computer and use it in GitHub Desktop.
Wrap (monkey-patch) Google Tag Manager push method

To console.log every argument pushed to Google Tag Manager, use

window.dataLayer = window.dataLayer || [];

// Save the original push method
const originalPush = window.dataLayer.push;

// Monkey patch push
window.dataLayer.push = function() {
    // Log each argument individually
    for (let i = 0; i < arguments.length; i++) {
        console.log('dataLayer.push argument: ', arguments[i]);
        // or console.log('dataLayer.push argument:', JSON.stringify(arguments[i]));
    }

    // Call original push with the same arguments
    return originalPush.apply(window.dataLayer, arguments);
};

NOTE: The window.dataLayer.push() method is commonly used with Google Tag Manager to send data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment