Skip to content

Instantly share code, notes, and snippets.

@Beraliv
Last active July 21, 2020 17:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Beraliv/158cc85b53277a332d7298a7563266c4 to your computer and use it in GitHub Desktop.
Save Beraliv/158cc85b53277a332d7298a7563266c4 to your computer and use it in GitHub Desktop.
BBC User API in html on bbc.co.uk
var USERINFO_URL = "";
var USERINFO_PREFIX = "www." || "www.";
function initUserCache(orbitUser) {
var availableAttr = [
'X-Country',
'X-Ip_is_advertise_combined',
'X-Ip_is_uk_combined'
];
var userCache = availableAttr.reduce(function(user, curr, i) {
if (!orbitUser[curr]) {
return user;
}
var res = Object.assign({}, user);
res[curr] = orbitUser[curr];
return res;
}, {});
if (Object.keys(userCache).length !== availableAttr.length) {
userCache._partial = true;
}
return userCache;
}
var user = initUserCache({
});
function checkStatus(response) {
if (response.status >= 200 && response.status < 300) {
return response;
} else {
var error = new Error(response.statusText);
error.response = response;
throw error;
}
}
function parseJSON(response) {
return response.json();
}
var supportsCors = function() {
return typeof XMLHttpRequest !== 'undefined' &&
'withCredentials' in new XMLHttpRequest();
};
var getUserInfoFallback = function() {
return window.bbcpage.loadModule(['orb/fig'])
.then(function(orbFig) {
return new Promise(function(resolve, reject) {
orbFig.load(function(fig) {
var getCountryFallback = function() {
if (fig.geo.isUK()) {
return 'GB';
} else if (fig.geo.isEU()) {
return 'EU';
}
};
resolve({
'X-Country': user.country || getCountryFallback(),
'X-Ip_is_uk_combined': fig().uk ? 'yes' : 'no',
'X-Ip_is_advertise_combined': fig().ad ? 'yes' : 'no'
});
}, function () {
reject('Error determining country. Timeout?');
});
});
});
};
var _userInfoRequest;
var getUserInfo = function() {
var host = window.location.host.toString().match(/bbc\.com$/) ? 'bbc.com' : 'bbc.co.uk';
var userInfoUrl = USERINFO_URL || window.location.protocol + "//" + USERINFO_PREFIX + host + '/userinfo';
if (!user._partial) {
return Promise.resolve(user);
}
if (!_userInfoRequest) {
if (typeof window.fetch === 'undefined' || !supportsCors()) {
_userInfoRequest = getUserInfoFallback();
} else {
_userInfoRequest = window.fetch(userInfoUrl, { credentials: 'same-origin' })
.then(checkStatus)
.then(parseJSON)
.then(function(userInfo) {
user = userInfo;
return user;
});
}
}
return _userInfoRequest;
};
window.bbcuser = {
getHashedId: function() {
return window.bbcpage.loadModule(['idcta-v2/idcta-1'])
.then(function(idcta) {
return idcta.getCookieInstance().getHidFromCookie();
});
},
getCountry: function() {
if (user['X-Country']) {
return Promise.resolve(user['X-Country']);
}
return getUserInfo().then(function(userInfo) {
return userInfo['X-Country'] || 'GB';
});
},
isUKCombined: function() {
return getUserInfo().then(function(userInfo) {
if (!userInfo['X-Ip_is_uk_combined']) {
throw new Error('missing isUKCombined from userinfo response');
}
return userInfo['X-Ip_is_uk_combined'].toLowerCase() === 'yes';
})
},
canSeeAdverts: function() {
return getUserInfo().then(function(userInfo) {
if (!userInfo['X-Ip_is_advertise_combined']) {
throw new Error('missing canSeeAdverts from userinfo response');
}
return userInfo['X-Ip_is_advertise_combined'].toLowerCase() === "yes";
});
},
isSignedIn: function() {
return window.bbcpage.loadModule(['idcta-v2/idcta-1'])
.then(function(idcta) {
return (idcta && idcta.getCookieInstance().hasCookie());
});
},
allowsPerformanceCookies: function() {
return window.bbcpage.loadModule(['orb/cookies'])
.then(function(bbccookies) {
return !!bbccookies.cookiesEnabled() && !!bbccookies.readPolicy('performance');
});
},
allowsFunctionalCookies: function() {
return window.bbcpage.loadModule(['orb/cookies'])
.then(function(bbccookies) {
return !!bbccookies.cookiesEnabled() && !!bbccookies.readPolicy('personalisation');
});
},
getCookieValue: function(cookieName) {
return window.bbcpage.loadModule(['orb/cookies'])
.then(function(bbccookies) {
return bbccookies.get(cookieName);
});
},
resetCookiesPreferences: function() {
return window.bbcpage.loadModule(['orb/cookies'])
.then(function(bbccookies) {
bbccookies.setDefaultCookiesSingleDomain();
});
},
hasCookiesEnabled: function() {
return window.bbcpage.loadModule(['orb/cookies'])
.then(function(bbccookies) {
return !!bbccookies.cookiesEnabled();
});
},
hasSeenCookieBanner: function() {
return window.bbcpage.loadModule(['orb/cookies'])
.then(function (bbccookies) {
return !!bbccookies.isCookiePolicySet();
});
},
logEvent: function (verb, noun, extraLabels) {
return window.bbcuser.hasCookiesEnabled()
.then(function(allowsCookies) {
if (allowsCookies) {
return window.bbcpage.loadModule(['istats-1'])
.then(function(istats) {
istats.log(verb, noun, extraLabels);
});
} else {
throw new Error('User cannot be tracked due to cookies preferences.');
}
});
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment