Skip to content

Instantly share code, notes, and snippets.

@keithws
Last active April 2, 2020 00:47
Show Gist options
  • Save keithws/dcfe7929a37dfb40f5bf96ff58ab9d9d to your computer and use it in GitHub Desktop.
Save keithws/dcfe7929a37dfb40f5bf96ff58ab9d9d to your computer and use it in GitHub Desktop.
Download a CSV file of all users with points in the Jive Rewards system for your community.
/*
* get users with points from Jive Rewards
* this code is meant to be injected into the browser console at
* https://rewards.jivesoftware.com/
* start by logging into your Jive Community and then
* visit the Jive Rewards Console (in your profile menu)
*
* after injection, it will add a "Download CSV" link in the page
* click the link to download a CSV of all Jive Users with Points
*/
(function () {
const JIVE_TENANT_ID = window.location.hash.split("/")[2];
/*
* helper function to download data
*/
function download (filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
function incrementPageNumber (url) {
let pageNumber = url.searchParams.get("pageNumber");
pageNumber = parseInt(pageNumber, 10);
pageNumber += 1;
url.searchParams.set("pageNumber", pageNumber);
}
function getUsers (url) {
console.log("fetching...");
console.log(url);
return fetch(url.href).then(response => response.json()).then(data => {
const usersWithPoints = data.users.filter(user => user.totalScore > 0);
usersWithPoints.forEach(user => {
jiveUsersWithPoints.push(`${user.totalScore},${user.username},${user.name},${user.userId},${user.profileUrl}`);
});
if (data.hasNextPage && usersWithPoints.length > 0) {
incrementPageNumber(url);
return getUsers(url);
} else {
return Promise.resolve(url);
}
});
}
function clickHandler (event) {
var url = new URL(`https://rewards.jivesoftware.com/api/tenants/${JIVE_TENANT_ID}/dashboard/usersPage`);
url.searchParams.set("pageNumber", 0);
url.searchParams.set("pageSize", 1000);
url.searchParams.set("sort", "totalScore");
url.searchParams.set("sortAscending", "false");
getUsers(url).then(() => {
console.log("done fetching");
console.log(`count: ${jiveUsersWithPoints.length}`);
// download CSV
download("Jive Users with Points.csv", jiveUsersWithPoints.join("\n"));
});
event.preventDefault();
}
var jiveUsersWithPoints = ["TOTAL_SCORE,USERNAME,NAME,EMAIL,PROFILE_URL"];
const element = document.querySelector(".j-leaderboard");
if (element) {
const link = document.createElement("a");
link.href = "#";
link.innerHTML = "Download CSV";
link.title = "Download CSV with all users with points";
link.addEventListener("click", clickHandler);
element.appendChild(link);
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment