Not sure but can be the problem of asynchronous concept, use the urlParams
after made it.
(window.onpopstate = function () {
......
......
urlParams = {};
while (match = search.exec(query)){
urlParams[decode(match[1])] = decode(match[2]);
}
callInit(urlParams); //use when it is made
})();
function callInit(urlParams){
ShopifyApp.init({
apiKey: "thiscontainsthecorrectapikeyvalue", //API Key
shopOrigin: 'https://' + urlParams["shop"],
debug: true
});
}