Google DFP in SPA (Angular, Vue) - how to destroy ad and all it's references to avoid memory leak Google DFP in SPA (Angular, Vue) - how to destroy ad and all it's references to avoid memory leak vue.js vue.js

Google DFP in SPA (Angular, Vue) - how to destroy ad and all it's references to avoid memory leak


We had a huge memory leak in our our vue web applicationWe found out we had event listeners on the window, seems like dfp created them.

    window.addEventHook = window.addEventListener;    window.addEventListener = function () {        if (!window.listenerHook)            window.listenerHook = [];        window.listenerHook.push({name: arguments[0], callback: arguments[1] });        window.addEventHook.apply(window,arguments);    };

We used this to save all the events listener being attached to window when the app first loaded, then when we want to remove dfp ads we iterate the array and execute window.removeEventListener on each of them (This will remove all window event listeners from window, You need to add checks to see you are not removing something important)

This solved our memory leak problem.