Instantly share code, notes, and snippets.

Embed
What would you like to do?
Show Google Analytics active users counter in document title and a notification whenever it changes.
/*
This script will display the Google Analytics active users count on
the tab's title and in a notification whenever it changes.
Usage: Open your browser's console when in a Google Analytics Realtime
tab and paste the following.
Reload the tab to discard this functionality.
*/
window.previousCount = "";
// Request permission for notifications.
Notification.requestPermission();
// Check for updated counter every 5 seconds
window.setInterval(function(){
var counter = $('#ID-overviewCounterValue');
// Don't do anything if there is no counter.
if (counter.length == 0) return;
// current count
var count = $('#ID-overviewCounterValue').text();
if(count != window.previousCount) {
window.previousCount = count;
document.title = "Active users: " + count;
if (Notification.permission === "granted") {
new Notification(document.title);
}
}
}, 5000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment