Skip to content

Commit

Permalink
Merge pull request #222 from glogiotatidis/campaign-block
Browse files Browse the repository at this point in the history
Add campaign based opt outs.
  • Loading branch information
glogiotatidis authored Dec 16, 2016
2 parents e8c90e9 + f360a47 commit 7664fbb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
32 changes: 27 additions & 5 deletions docs/developing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,12 @@ to trigger more events. For example to trigger Firefox Accounts:
Snippet Block List
^^^^^^^^^^^^^^^^^^

Snippets can be prevented from showing using a block list. By default the block list is empty and the intention is to allow users to block specific snippets from showing by taking an action. Snippet service automatically assigns the block functionality to all elements of snippet with class `block-snippet-button`. For example a disruptive snippet can include a special `Do not display again` link that adds the snippet into the block list:
Snippets can be prevented from showing using a block list. By default the block
list is empty and the intention is to allow users to block specific snippets
from showing by taking an action. Snippet service automatically assigns the
block functionality to all elements of snippet with class
`block-snippet-button`. For example a disruptive snippet can include a special
`Do not display again` link that adds the snippet into the block list:

.. code-block:: html

Expand All @@ -243,7 +248,8 @@ If you need more control you can directly access the low-level function `addToBl
(function () {
var link = document.getElementById('block-snippet-link');
link.onclick = function() {
addToBlockList([[snippet_id]]);
// Add currently showing snippet to block list
addToBlockList();
window.location.reload();
}
})();
Expand All @@ -257,9 +263,25 @@ If you need more control you can directly access the low-level function `addToBl

More low level functions are `popFromBlockList` and `getBlockList`.

In bug `1172579`_ close button assets are provided to build a image
button in your snippet. Refer to the `simple snippet`_ code on how to
do this.
Function `addToBlockList` will by default add to block list the *campaign* of
the current snippet thus preventing all snippets -even the ones created in the
future- with the same campaign name to get blocked. This is particularly useful
when we do A/B testing. A user who blocked a variation of a snippet will not see
any of the variations either, as long as they share the same snippet campaign.

If there's no campaign set for showing snippet `addToBlockList` will block the
ID from the snippet.

The function also accepts an argument to explicitly set the blocking value::

.. code-html::
addToBlockList('foo-bar');

Will add `foo-bar` to block list instead of the showing snippet's campaign
or ID.

In bug `1172579`_ close button assets are provided to build a image button in
your snippet. Refer to the `simple snippet`_ code on how to do this.



Expand Down
3 changes: 2 additions & 1 deletion snippets/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,8 @@ class Snippet(CachingMixin, SnippetBaseModel):

campaign = models.CharField(
max_length=255, blank=True, default='',
help_text='Optional campaign name. Will be added in the stats ping.')
help_text=('Optional campaign name. Will be added in the stats ping. '
'Will be used for snippet blocking if set.'))
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)

Expand Down
14 changes: 8 additions & 6 deletions snippets/base/templates/base/includes/snippet.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,8 @@ Mozilla.UITour.setConfiguration = function(configName, configValue) {
var blockList = getBlockList();
snippets = snippets.filter(
function (snippet) {
if (blockList.indexOf(snippet.id) === -1) {
var blockID = snippet.campaign || snippet.id;
if (blockList.indexOf(blockID) === -1) {
return true;
}
return false;
Expand Down Expand Up @@ -503,12 +504,11 @@ Mozilla.UITour.setConfiguration = function(configName, configValue) {

// Add links to buttons that block snippets.
function addSnippetBlockLinks(elements) {
var snippet_id = ABOUTHOME_SHOWN_SNIPPET.id;
var blockSnippet = function (event) {
event.preventDefault();
event.stopPropagation();

addToBlockList(snippet_id);
addToBlockList();

// Waiting for 500ms before reloading the page after user blocks a snippet,
// will allow the IndexedDB more time to complete its transaction. While
Expand All @@ -519,7 +519,7 @@ Mozilla.UITour.setConfiguration = function(configName, configValue) {
function reloadWindow() {
window.location.reload();
}
sendMetric('snippet-blocked', function() { setTimeout(reloadWindow, 500); })
sendMetric('snippet-blocked', function() { setTimeout(reloadWindow, 500); });
};

for (var k = 0; k < elements.length; k++) {
Expand Down Expand Up @@ -586,7 +586,7 @@ Mozilla.UITour.setConfiguration = function(configName, configValue) {
event.preventDefault();
var callback = function() {
target.click();
}
};
}
sendMetric(metric, callback, target.href);
}
Expand Down Expand Up @@ -661,7 +661,9 @@ function popFromBlockList(snippetID) {

function addToBlockList(snippetID) {
var blockList = getBlockList();
snippetID = parseInt(snippetID, 10);
if (snippetID === undefined) {
snippetID = ABOUTHOME_SHOWN_SNIPPET.campaign || ABOUTHOME_SHOWN_SNIPPET.id;
}
if (blockList.indexOf(snippetID) === -1) {
blockList = [snippetID].concat(blockList);
gSnippetsMap.set('blockList', blockList);
Expand Down

0 comments on commit 7664fbb

Please sign in to comment.