{"__v":30,"_id":"55e5a335a603d62300f8926f","category":{"__v":9,"_id":"55e58d16f493c11700584314","pages":["55e58d21f11ba80d003238be","55e59fddd101191900f30b82","55e5a218f11ba80d00323911","55e5a28bf11ba80d00323915","55e5a2adf11ba80d00323917","55e5a335a603d62300f8926f","55e5a4cad101191900f30b9d","55e5a5396015ce1900eadb54","55e81c700f5df10d004a9c10"],"project":"55e58b63d101191900f30b13","version":"55e58b63d101191900f30b16","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-09-01T11:33:42.203Z","from_sync":false,"order":1,"slug":"js-api","title":"JavaScript API"},"parentDoc":null,"project":"55e58b63d101191900f30b13","user":"5554788cb450630d00041dbc","version":{"__v":3,"_id":"55e58b63d101191900f30b16","project":"55e58b63d101191900f30b13","createdAt":"2015-09-01T11:26:27.668Z","releaseDate":"2015-09-01T11:26:27.668Z","categories":["55e58b65d101191900f30b17","55e58d16f493c11700584314","55e58d1bd101191900f30b1d"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-09-01T13:08:05.417Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":6,"body":"Subscribers can be categorized into different segments. This allows you to categorize and target users who have similar behavior on your website. For example :- for all users who get subscribed to push notifications from the homepage, you may want to put them into a segment 'homepage'. Later on, you can use the REST API/our dashboard to send notifications to subscribers of that particular segment only.\n\n**Notes:**\n\n1) A subscriber can belong to multiple segments.\n2) This operation will only work reliably if it's placed inside the [APIReady callback](http://api.pushcrew.com/docs/is-api-ready). (Example given below)\n3) This operation will only put the subscriber in the segment once the API gets ready(on page load). If a new user subscribes, you can use the same operation inside the [subscriptionSuccessCallback](http://api.pushcrew.com/docs/successful-subscription-callback) (Example given below)\n\n**Parameters:**\n\n1) addSubscriberToSegment: Command to add the subscriber to the specified segment\n2) segmentName: This is a string used to identify the segment. Maximum length of segment name can be 30 chars and it can only contain alphanumeric characters, underscore and dash.\n3) callbackFunctionName: (Optional)\nName of the function to be executed when the subscriber gets added to the segment successfully. This is optional.\n\nExample usage:\n\n_pcq.push(['addSubscriberToSegment', 'homepage', callbackFunction]); \n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"window._pcq = window._pcq || [];\\n_pcq.push(['APIReady', callbackOnAPIReady]); //will execute callback function when PushCrew API is ready\\n\\nfunction callbackOnAPIReady() {\\n    //now api is ready\\n    _pcq.push(['addSubscriberToSegment', 'homepage', callbackForAddToSegment]);\\n}\\n\\nfunction callbackForAddToSegment(response) {\\n    if(response === -1) {\\n        console.log('User is not a subscriber or has blocked notifications');\\n    }\\n  \\n    if(response === false) {\\n        console.log('Segment name provided is not valid. Maximum length of segment name can be 30 chars and it can only contain alphanumeric characters, underscore and dash.');\\n    }\\n  \\n    if(response === true) {\\n        console.log('User got added to the segment successfully. Now you may run any code you wish to execute after user gets added to segment successfully');\\n    }\\n}\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nIf the user needs to be put in a segment as soon as he/she subscribes, here is some example code:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"window._pcq = window._pcq || [];\\n_pcq.push(['APIReady', callbackOnAPIReady]); //will execute callback function when PushCrew API is ready\\n\\n_pcq.push(['subscriptionSuccessCallback',callbackOnSuccessfulSubscription]); //registers callback function to be called when user gets successfully subscribed\\n\\nfunction callbackOnAPIReady() {\\n    //now api is ready\\n    _pcq.push(['addSubscriberToSegment', 'homepage', callbackForAddToSegment]);\\n}\\n\\nfunction callbackOnSuccessfulSubscription(subscriberId, values) {\\n    //user just got subscribed\\n    _pcq.push(['addSubscriberToSegment', 'homepage', callbackForAddToSegment]);\\n}\\n\\nfunction callbackForAddToSegment(response) {\\n    if(response === -1) {\\n        console.log('User is not a subscriber or has blocked notifications');\\n    }\\n  \\n    if(response === false) {\\n        console.log('Segment name provided is not valid. Maximum length of segment name can be 30 chars and it can only contain alphanumeric characters, underscore and dash.');\\n    }\\n  \\n    if(response === true) {\\n        console.log('User got added to the segment successfully. Now you may run any code you wish to execute after user gets added to segment successfully');\\n    }\\n}\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nIf you want to add subscribers into segment depending upon the url(specific keyword in the url) they visit, here is some sample code:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<script type=\\\"text/javascript\\\">\\n\\nwindow._pcq = window._pcq || [];\\n_pcq.push(['APIReady', callbackOnAPIReady]); //will execute callback function when PushCrew API is ready\\n\\n_pcq.push(['subscriptionSuccessCallback',callbackOnSuccessfulSubscription]); //registers callback function to be called when user gets successfully subscribed\\n\\nfunction addToAppropriateSegment() {\\n    if(window.location.href.indexOf(\\\"category1\\\") > -1) {\\n        _pcq.push(['addSubscriberToSegment', 'category1']);\\n    }\\n    else if(window.location.href.indexOf(\\\"category2\\\") > -1) {\\n        _pcq.push(['addSubscriberToSegment', 'category2']);\\n    }\\n    else if(window.location.href.indexOf(\\\"category3\\\") > -1) {\\n        _pcq.push(['addSubscriberToSegment', 'category3']);\\n    }\\n}\\n\\nfunction callbackOnAPIReady() {\\n   addToAppropriateSegment();\\n}\\n\\nfunction callbackOnSuccessfulSubscription(subscriberId, values) {\\n   addToAppropriateSegment();\\n}\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]","excerpt":"","slug":"add-subscriber-to-segment","type":"fn","title":"Add Subscriber to a Particular Segment"}

fnAdd Subscriber to a Particular Segment


Subscribers can be categorized into different segments. This allows you to categorize and target users who have similar behavior on your website. For example :- for all users who get subscribed to push notifications from the homepage, you may want to put them into a segment 'homepage'. Later on, you can use the REST API/our dashboard to send notifications to subscribers of that particular segment only. **Notes:** 1) A subscriber can belong to multiple segments. 2) This operation will only work reliably if it's placed inside the [APIReady callback](http://api.pushcrew.com/docs/is-api-ready). (Example given below) 3) This operation will only put the subscriber in the segment once the API gets ready(on page load). If a new user subscribes, you can use the same operation inside the [subscriptionSuccessCallback](http://api.pushcrew.com/docs/successful-subscription-callback) (Example given below) **Parameters:** 1) addSubscriberToSegment: Command to add the subscriber to the specified segment 2) segmentName: This is a string used to identify the segment. Maximum length of segment name can be 30 chars and it can only contain alphanumeric characters, underscore and dash. 3) callbackFunctionName: (Optional) Name of the function to be executed when the subscriber gets added to the segment successfully. This is optional. Example usage: _pcq.push(['addSubscriberToSegment', 'homepage', callbackFunction]); [block:code] { "codes": [ { "code": "window._pcq = window._pcq || [];\n_pcq.push(['APIReady', callbackOnAPIReady]); //will execute callback function when PushCrew API is ready\n\nfunction callbackOnAPIReady() {\n //now api is ready\n _pcq.push(['addSubscriberToSegment', 'homepage', callbackForAddToSegment]);\n}\n\nfunction callbackForAddToSegment(response) {\n if(response === -1) {\n console.log('User is not a subscriber or has blocked notifications');\n }\n \n if(response === false) {\n console.log('Segment name provided is not valid. Maximum length of segment name can be 30 chars and it can only contain alphanumeric characters, underscore and dash.');\n }\n \n if(response === true) {\n console.log('User got added to the segment successfully. Now you may run any code you wish to execute after user gets added to segment successfully');\n }\n}", "language": "javascript" } ] } [/block] If the user needs to be put in a segment as soon as he/she subscribes, here is some example code: [block:code] { "codes": [ { "code": "window._pcq = window._pcq || [];\n_pcq.push(['APIReady', callbackOnAPIReady]); //will execute callback function when PushCrew API is ready\n\n_pcq.push(['subscriptionSuccessCallback',callbackOnSuccessfulSubscription]); //registers callback function to be called when user gets successfully subscribed\n\nfunction callbackOnAPIReady() {\n //now api is ready\n _pcq.push(['addSubscriberToSegment', 'homepage', callbackForAddToSegment]);\n}\n\nfunction callbackOnSuccessfulSubscription(subscriberId, values) {\n //user just got subscribed\n _pcq.push(['addSubscriberToSegment', 'homepage', callbackForAddToSegment]);\n}\n\nfunction callbackForAddToSegment(response) {\n if(response === -1) {\n console.log('User is not a subscriber or has blocked notifications');\n }\n \n if(response === false) {\n console.log('Segment name provided is not valid. Maximum length of segment name can be 30 chars and it can only contain alphanumeric characters, underscore and dash.');\n }\n \n if(response === true) {\n console.log('User got added to the segment successfully. Now you may run any code you wish to execute after user gets added to segment successfully');\n }\n}", "language": "javascript" } ] } [/block] If you want to add subscribers into segment depending upon the url(specific keyword in the url) they visit, here is some sample code: [block:code] { "codes": [ { "code": "<script type=\"text/javascript\">\n\nwindow._pcq = window._pcq || [];\n_pcq.push(['APIReady', callbackOnAPIReady]); //will execute callback function when PushCrew API is ready\n\n_pcq.push(['subscriptionSuccessCallback',callbackOnSuccessfulSubscription]); //registers callback function to be called when user gets successfully subscribed\n\nfunction addToAppropriateSegment() {\n if(window.location.href.indexOf(\"category1\") > -1) {\n _pcq.push(['addSubscriberToSegment', 'category1']);\n }\n else if(window.location.href.indexOf(\"category2\") > -1) {\n _pcq.push(['addSubscriberToSegment', 'category2']);\n }\n else if(window.location.href.indexOf(\"category3\") > -1) {\n _pcq.push(['addSubscriberToSegment', 'category3']);\n }\n}\n\nfunction callbackOnAPIReady() {\n addToAppropriateSegment();\n}\n\nfunction callbackOnSuccessfulSubscription(subscriberId, values) {\n addToAppropriateSegment();\n}", "language": "javascript" } ] } [/block]