Skip to content

Commit

Permalink
Merge pull request #8653 from piwik/8650
Browse files Browse the repository at this point in the history
Fixes #8650, add method logAllContentBlocksOnPage to piwik.js JavaScript tracker that let's you debug which content blocks are on your page.
  • Loading branch information
diosmosis committed Aug 31, 2015
2 parents 1b91a51 + 84a9454 commit 65afcee
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -19,6 +19,10 @@ This is a changelog for Piwik platform developers. All changes for our HTTP API'
* `Tracker.recordEcommerceGoal`
* `Tracker.recordStandardGoals`

### New APIs

* The JavaScript Tracker `piwik.js` got a new method `logAllContentBlocksOnPage` to log all found content blocks within a page to the console. This is useful to debug / test content tracking. It can be triggered via `_paq.push(['logAllContentBlocksOnPage'])`

### Internal Change
* The option `branch` of the console command `development:sync-system-test-processed` was removed as it is no longer needed.

Expand Down
17 changes: 16 additions & 1 deletion js/piwik.js
Expand Up @@ -455,7 +455,7 @@ if (typeof JSON2 !== 'object') {
getTrackedContentImpressions, getCurrentlyVisibleContentImpressionsRequestsIfNotTrackedYet,
contentInteractionTrackingSetupDone, contains, match, pathname, piece, trackContentInteractionNode,
trackContentInteractionNode, trackContentImpressionsWithinNode, trackContentImpression,
enableTrackOnlyVisibleContent, trackContentInteraction, clearEnableTrackOnlyVisibleContent,
enableTrackOnlyVisibleContent, trackContentInteraction, clearEnableTrackOnlyVisibleContent, logAllContentBlocksOnPage,
trackVisibleContentImpressions, isTrackOnlyVisibleContentEnabled, port, isUrlToCurrentDomain,
isNodeAuthorizedToTriggerInteraction, replaceHrefIfInternalLink, getConfigDownloadExtensions, disableLinkTracking,
substr, setAnyAttribute, wasContentTargetAttrReplaced, max, abs, childNodes, compareDocumentPosition, body,
Expand All @@ -474,6 +474,7 @@ if (typeof JSON2 !== 'object') {
/*members amd */
/*global console:true */
/*members error */
/*members log */

// asynchronous tracker (or proxy)
if (typeof _paq !== 'object') {
Expand Down Expand Up @@ -5318,6 +5319,20 @@ if (typeof Piwik !== 'object') {
});
},

/**
* Useful to debug content tracking. This method will log all detected content blocks to console
* (if the browser supports the console). It will list the detected name, piece, and target of each
* content block.
*/
logAllContentBlocksOnPage: function () {
var contentNodes = content.findContentNodes();
var contents = content.collectContent(contentNodes);

if (console !== undefined && console && console.log) {
console.log(contents);
}
},

/**
* Records an event
*
Expand Down
4 changes: 2 additions & 2 deletions piwik.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

100 changes: 98 additions & 2 deletions tests/javascript/index.php
Expand Up @@ -1926,7 +1926,7 @@ function assertTrackingRequest(actual, expectedStartsWith, message)
});

test("API methods", function() {
expect(65);
expect(66);

equal( typeof Piwik.addPlugin, 'function', 'addPlugin' );
equal( typeof Piwik.getTracker, 'function', 'getTracker' );
Expand Down Expand Up @@ -1997,6 +1997,7 @@ function assertTrackingRequest(actual, expectedStartsWith, message)
equal( typeof tracker.trackContentImpressionsWithinNode, 'function', 'trackContentImpressionsWithinNode' );
equal( typeof tracker.trackContentInteraction, 'function', 'trackContentInteraction' );
equal( typeof tracker.trackContentInteractionNode, 'function', 'trackContentInteractionNode' );
equal( typeof tracker.logAllContentBlocksOnPage, 'function', 'logAllContentBlocksOnPage' );
// ecommerce
equal( typeof tracker.setEcommerceView, 'function', 'setEcommerceView' );
equal( typeof tracker.addEcommerceItem, 'function', 'addEcommerceItem' );
Expand Down Expand Up @@ -3190,7 +3191,7 @@ function getLastInteractionType() { return lastInteractionType; }
});

test("trackingContent", function() {
expect(81);
expect(83);

function assertTrackingRequest(actual, expectedStartsWith, message)
{
Expand Down Expand Up @@ -3526,6 +3527,101 @@ function resetTracker(track, token, replace)
start();
}, 7000);

expected =
[
{
"name": "My Ad 7",
"piece": "Unknown",
"target": "http://img7.example.com"
},
{
"name": "http://www.example.com/path/xyz.jpg",
"piece": "http://www.example.com/path/xyz.jpg",
"target": "http://img6.example.com"
},
{
"name": "My Ad 5",
"piece": "http://img5.example.com/path/xyz.jpg",
"target": origin + "/anylink5"
},
{
"name": "My content 4",
"piece": "My content 4",
"target": "http://img4.example.com"
},
{
"name": toAbsolutePath("img3-en.jpg"),
"piece": toAbsoluteUrl("img3-en.jpg"),
"target": "http://img3.example.com"
},
{
"name": "img.jpg",
"piece": "img.jpg",
"target": "http://img2.example.com"
},
{
"name": toAbsolutePath("img1-en.jpg"),
"piece": toAbsoluteUrl("img1-en.jpg"),
"target": ""
},
{
"name": "/tests/javascript/img1-en.jpg",
"piece": toAbsoluteUrl("img1-en.jpg"),
"target": ""
}];

var consoleOld = console;
var loggedContentBlocks = [];
console = {log: function (content){
loggedContentBlocks = content;
}};
tracker.logAllContentBlocksOnPage();
console = consoleOld;
expected =
[
{
"name": "My Ad 7",
"piece": "Unknown",
"target": "http://img7.example.com"
},
{
"name": "http://www.example.com/path/xyz.jpg",
"piece": "http://www.example.com/path/xyz.jpg",
"target": "http://img6.example.com"
},
{
"name": "My Ad 5",
"piece": "http://img5.example.com/path/xyz.jpg",
"target": origin + "/anylink5"
},
{
"name": "My content 4",
"piece": "My content 4",
"target": "http://img4.example.com"
},
{
"name": toAbsolutePath("img3-en.jpg"),
"piece": toAbsoluteUrl("img3-en.jpg"),
"target": "http://img3.example.com"
},
{
"name": "img.jpg",
"piece": "img.jpg",
"target": "http://img2.example.com"
},
{
"name": toAbsolutePath("img1-en.jpg"),
"piece": toAbsoluteUrl("img1-en.jpg"),
"target": ""
},
{
"name": "/tests/javascript/img1-en.jpg",
"piece": toAbsoluteUrl("img1-en.jpg"),
"target": ""
}];

equal(expected.length, loggedContentBlocks.length, 'logAllContentBlocksOnPage should detect correct number of content blocks');
equal(JSON.stringify(expected), JSON.stringify(loggedContentBlocks), 'logAllContentBlocksOnPage should log all content blocks');
});

test("trackingContentInteractionInteractive", function() {
Expand Down

0 comments on commit 65afcee

Please sign in to comment.