Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
164 lines (151 sloc) 5.3 KB
<!DOCTYPE html>
<html>
<head>
<title>OneSignal Tagging Example</title>
<link rel="manifest" href="/manifest.json" />
<script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" async=""></script>
<script>
var OneSignal = window.OneSignal || [];
OneSignal.push(function() {
OneSignal.init({
appId: "3beb3078-e0f1-4629-af17-fde833b9f716",
});
});
</script>
</head>
<body>
<h1> OneSignal Tagging Examples </h1>
<!-- -------------------------------------------------------------- -->
<!-- Tag upon subscription -->
<script>
let page_author = 'the author';
let page_topic = 'sports';
OneSignal.push(function() {
OneSignal.on('subscriptionChange', function(isSubscribed) {
if (isSubscribed === true) {
console.log('The user subscription state is now:', isSubscribed);
OneSignal.sendTags({
"page_url": window.location.href,
"author": page_author,
"topic": page_topic,
}).then(function(tagsSent) {
// Callback called when tags have finished sending
console.log(tagsSent);
});
}
});
});
</script>
<!-- END Tag upon supbscription -->
<!-- -------------------------------------------------------------- -->
<!-- Tag upon button click -->
<div>
<button type="button" onclick="tagButtonClick()">Tag VIP User</button>
<button type="button" onclick="checkTagButtonClick()">Check and Update Tags</button>
<button type="button" onclick="deleteTagButtonClick()">Delete Tag</button>
<button type="button" onclick="timeOperatorButtonPress()">Time Operator</button>
</div>
<script>
// Tag User
function tagButtonClick() {
console.log("VIP Tag button clicked");
OneSignal.push(function() {
OneSignal.sendTag("user_type", "vip").then(function(tagsSent) {
console.log("tagsSent: " + tagsSent.user_type);
});
});
}
// Check and update Tags
function checkTagButtonClick() {
console.log("Check Tag button clicked");
OneSignal.push(function() {
OneSignal.getTags(function(tags) {
console.log("This user has tags: " + JSON.stringify(tags));
if (tags.user_type === "vip") {
console.log("change vip to basic");
OneSignal.sendTag("user_type", "basic");
} else if (tags.user_type === "basic") {
console.log("found basic, set to empty string to delete");
OneSignal.sendTag("user_type", "");
}
});
});
}
// Delete Tags
function deleteTagButtonClick() {
console.log("Delete Tag button clicked");
OneSignal.push(function() {
OneSignal.deleteTags(["user_type", "sub_url", "topic"]);
});
}
// time operator
function timeOperatorButtonPress() {
console.log("Time Operator Tag button clicked");
OneSignal.push(function() {
let timestamp = Math.floor(Date.now() / 1000);
OneSignal.sendTag("event_key", timestamp).then(function(tagsSent) {
// Callback called when tags have finished sending
console.log(tagsSent);
});
});
}
</script>
<!-- Tag upon notification click -->
<script type="text/javascript">
OneSignal.push(["addListenerForNotificationOpened", function(payload) {
// Track last notification click time (paid feature only)
let timestamp = Math.floor(Date.now() / 1000);
OneSignal.sendTag("ln_click", timestamp);
console.log("Received Payload Data: ", payload.data);
let topic = payload.data.topic; // set topic in notification
if (topic) {
OneSignal.getTags().then(function(tags) {
var topicCount = parseInt(tags[topic], 10);
console.log("topicCount: ", topicCount);
if (!isNaN(topicCount)) {
topicCount += 1;
} else {
topicCount = 1;
}
OneSignal.sendTag(topic, topicCount).then(function(tagsSent) {
console.log("tagsSent: ", JSON.stringify(tagsSent));
});
});
}
}]);
</script>
<!-- END Tag upon notificatin click -->
<!-- -------------------------------------------------------------- -->
<!-- Tag upon 3rd visit -->
<script>
var numVisitsTrigger = 3; /* Number of page visits before tagging user */
var topic = "sports"; /* The topic of the page */
function tagUserWithPageTopic(key, value) {
OneSignal.push(function() {
OneSignal.isPushNotificationsEnabled(function(isEnabled) {
console.log("isEnabled: ", isEnabled);
if (isEnabled) {
OneSignal.sendTag(key, value, function(tagsSent) {
// Callback called when tags have finished sending
console.log('tags sent: ', tagsSent);
});
}
});
});
}
if (typeof localStorage !== "undefined") {
var topicVisits = parseInt(localStorage.getItem(topic), 10);
if (!isNaN(topicVisits)) {
topicVisits += 1;
} else {
topicVisits = 0;
}
localStorage.setItem(topic, topicVisits)
if (topicVisits >= numVisitsTrigger) {
tagUserWithPageTopic(topic, topicVisits);
}
}
</script>
<!-- END TAG 3rd visit -->
</body>
</html>
You can’t perform that action at this time.