Skip to content

Make JSON usage in piwik.js a bit more stable and reduce conflict with other libraries #11023

@tsteur

Description

@tsteur

Some sites seem to still have problems with errors like JSON2.stringify is not a function. We already fixed some compatibility issues with prototype library etc.

I think what would make it safer is to no longer use JSON2 maybe, but instead eg JSON_PIWIK or some similar naming? This prevents the case where JSON2 is overwritten by another lib at a later point after Piwik is loaded.

Then also we could also recognize JSON 3 and prefer usage of window.json if defined.

if (typeof window.JSON==="object" && window.JSON.stringify && window.JSON.parse){
    JSON_PIWIK=window.JSON; 
} else if(typeof JSON2==="object" && JSON2.stringify && JSON2.parse) { 
  JSON_PIWIK = JSON2
} else if(typeof JSON3==="object"&&JSON3.stringify&&JSON3.parse) {
  JSON_PIWIK=JSON3;
} else {
 JSON_PIWIK = JSON2 = {... the JSON 2 version ...};
 // we could remove this part but would break some tracking features in IE7. If they still wanted to track in IE 7 and lower, we could ask sites to include JSON 2 or JSON 3 themselves
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementFor new feature suggestions that enhance Matomo's capabilities or add a new report, new API etc.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions