Recently, I get this postMessage couldn't be cloned error. It is happening on most of the latest browsers like Chrome 68, Firefox 61.0, IE11, Edge.
Failed to execute 'postMessage' on 'Window':
function (a){if(qe.$a.hasOwnProperty(a))return qe.$a[a]}
could not be cloned.
The stack trace is:
Error: Failed to execute 'postMessage' on 'Window':
function (a){if(qe.$a.hasOwnProperty(a))return qe.$a[a]}
could not be cloned.
at _reportEvent (eval at (:1:35637), :94:35)
at eval (eval at (:1:35637), :55:5)
at eval (eval at (:1:35637), :433:11)
Searching through the source of my page in DevTools shows gtm.js
as the source of the code fragment:
I have a Google Tag Manager tracking code on my page. Why is this happening?
This happens all the time, if something can not be duplicated by the structured clone algorithm. This algorithm is used by
window.postMessage
. If we read the documentation fromwindow.postMessage
for the first parameter:and then open the description from structured clone algorithm (see last link above) then we can read:
I tested it with some objects and I can show you following examples when this is happening...
Example with custom function
Example with native function
The same we will see with native functions like
Boolean
,Date
,String
,RegExp
,Number
,Array
.Example with native object
Example with HTML element object
We could write more examples if we read the description from The structured clone algorithm above, but I think here it is enough.
What we could do to avoid this error
In our code we could use only supported types (see the list above) in our objects. But in not our code we have to contact the developers from this code and write them how they have to correct their code. In the case from the Google Tag Manager you could write it to the Official Google Tag Manager Forum with description how they have to correct their code.
Workaround for some browsers
In some browsers you can not override native methods for security reasons. For example IE does not allow to override
window.postMessage
. But other browsers like Chrome allow to override this method like follows:But note that
window
is a global object of JavaScript context and it is not created from theprototype
. In other words: you can not override it withwindow.prototype.postMessage = ...
.Example with workaround
How to implement this workaround
Please put this overriden
window.postMessage
function in script part in your HTML page before Google Tag Manager script. But in better way you could help the developers from Google Tag Manager to understand and to correct this error and you can wait for corrected Google Tag Manager script.These errors are caused by Facebook crawlers executing JavaScript code.
I have had occurrences of this error from these IPs (all in the Facebook IP ranges) and user agents:
To get an up to date list of Facebook crawler IPs, see this command from https://developers.facebook.com/docs/sharing/webmasters/crawler/ :
whois -h whois.radb.net -- '-i origin AS32934' | grep ^route
You will need to update your error reporting mechanism to filter out errors from these IP ranges.
You could do this on the client side in JavaScript by determining the user's IP address upon an error (see How to get client's IP address using JavaScript?).
Or you could do this on the server side. Here is an example for ASP.NET MVC: