Skip to content

Commit

Permalink
Merge pull request #108 from glogiotatidis/blocklist
Browse files Browse the repository at this point in the history
 [bug 1155107] Filter against BlockList.
  • Loading branch information
glogiotatidis committed Apr 27, 2015
2 parents 53b2cd9 + 41c148e commit 1c56b58
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 1 deletion.
63 changes: 63 additions & 0 deletions docs/developing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,69 @@ The syntax in a snippet is slightly different and uses square brackets `[[snippe
.. warning:: Beware that in this case spacing matters and `[[ snippet_id ]]` will not work.


Custom Metric Pings
^^^^^^^^^^^^^^^^^^^

Snippet events can be captured and send to a metrics server. By default an `impression` ping gets triggered when a snippet is shown. Pings are sampled and only 10% of the total pings get send to the server.

Snippet developers can send custom pings to capture interactions, clicks and other interesting actions using the `sendMetric` function like this:


.. code-block:: html

<!-- Use Raw Template to try this out -->
<div class="snippet" id="ping-snippet-[[ snippet_id ]]">
<p class="message">Foo!</p>
</div>
<script type="text/javascript">
//<![CDATA[
(function() {
var snippet = document.getElementById('ping-snippet-[[ snippet_id ]]');
snippet.addEventListener('show_snippet', function() {
(function () {
var callback = function() {
alert('Success!');
};
var metric_name = 'success-ping-[[ snippet_id ]]';
sendMetric(metric_name, callback);
})();
}, false);
})();
//]]>
</script>

.. note:: Callback function is optional.


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. 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

<!-- Use Raw Template to try this out -->
<div class="snippet" id="block-snippet-[[snippet_id]]">
Foo! <a href="#" id="block-snippet-link">Do not show again</a>
</div>
<script type="text/javascript">
//<![CDATA[
(function() {
var snippet = document.getElementById('block-snippet-[[snippet_id]]');
snippet.addEventListener('show_snippet', function() {
(function () {
var link = document.getElementById('block-snippet-link');
link.onclick = function() {
addToBlockList([[snippet_id]]);
window.location.reload();
}
})();
}, false);
})();
//]]>
</script>

.. _testing:

Testing
Expand Down
37 changes: 36 additions & 1 deletion snippets/base/templates/base/includes/snippet_js.html
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@
)
}


// FxAccount is already setup skip snippets that link to
// about:accounts
if (isFxAccountSetup()) {
Expand All @@ -159,6 +158,18 @@
);
}

// Exclude snippets in block list.
var blockList = getBlockList();
snippets = snippets.filter(
function (snippet) {
var snippet_id = parseInt(snippet.parentNode.getAttribute('data-snippet-id'), 10);
if (blockList.indexOf(snippet_id) === -1) {
return true;
}
return false;
}
);

// Choose a random snippet from the snippets list.
if (snippets && snippets.length) {
var sum = 0;
Expand Down Expand Up @@ -397,5 +408,29 @@
r.send();
return r;
}

function popFromBlockList(snippetID) {
var blockList = getBlockList();
var item = blockList.pop(snippetID);
gSnippetsMap.set('blockList', blockList);
return item;
}

function addToBlockList(snippetID) {
var blockList = getBlockList();
snippetID = parseInt(snippetID, 10);
if (blockList.indexOf(snippetID) === -1) {
blockList.unshift(snippetID);
gSnippetsMap.set('blockList', blockList);
}
}

function getBlockList() {
if (gSnippetsMap.get('blockList') === undefined) {
gSnippetsMap.set('blockList', []);
}
return gSnippetsMap.get('blockList');
}

//]]>
</script>

0 comments on commit 1c56b58

Please sign in to comment.