Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Fixed Facebook social interaction tracking. #240

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 36 additions & 49 deletions lib/plugins/social-widget-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ class SocialWidgetTracker {
this.addTwitterEventHandlers = this.addTwitterEventHandlers.bind(this);
this.handleTweetEvents = this.handleTweetEvents.bind(this);
this.handleFollowEvents = this.handleFollowEvents.bind(this);
this.handleLikeEvents = this.handleLikeEvents.bind(this);
this.handleUnlikeEvents = this.handleUnlikeEvents.bind(this);
this.handleFacebookEvents = this.handleFacebookEvents.bind(this);

this.queue = TrackerQueue.getOrCreate(tracker.get('trackingId'));

Expand All @@ -78,7 +77,7 @@ class SocialWidgetTracker {
*/
addWidgetListeners() {
this.queue.pushTask(() => {
if (window.FB) this.addFacebookEventHandlers();
this.addFacebookEventHandlers();
if (window.twttr) this.addTwitterEventHandlers();
});
}
Expand Down Expand Up @@ -119,25 +118,15 @@ class SocialWidgetTracker {
* official Facebook like button.
*/
addFacebookEventHandlers() {
try {
window.FB.Event.subscribe('edge.create', this.handleLikeEvents);
window.FB.Event.subscribe('edge.remove', this.handleUnlikeEvents);
} catch (err) {
// Do nothing.
}
window.addEventListener('blur', this.handleFacebookEvents);
}

/**
* Removes event handlers for the "like" and "unlike" events emitted by the
* official Facebook like button.
*/
removeFacebookEventHandlers() {
try {
window.FB.Event.unsubscribe('edge.create', this.handleLikeEvents);
window.FB.Event.unsubscribe('edge.remove', this.handleUnlikeEvents);
} catch (err) {
// Do nothing.
}
window.removeEventListener('blur', this.handleFacebookEvents);
}

/**
Expand Down Expand Up @@ -193,41 +182,39 @@ class SocialWidgetTracker {
}

/**
* Handles `like` events emitted by the Facebook JS SDK.
* @param {string} url The URL corresponding to the like event.
* Handles events of embedded Facebook `like` and `share` widgets.
*
* Due to privacy issues Facebook decided to deprecate a particular solution
* which allows to track clicks of native buttons embedded by Facebook JS SDK.
* @param {Event} event The window blur event.
*
* @see https://developers.facebook.com/blog/post/2017/11/07/changes-developer-offerings/
*/
handleLikeEvents(url) {
this.queue.pushTask(({time}) => {
/** @type {FieldsObj} */
const defaultFields = {
transport: 'beacon',
socialNetwork: 'Facebook',
socialAction: 'like',
socialTarget: url,
queueTime: now() - time,
};
this.tracker.send('social', createFieldsObj(defaultFields,
this.opts.fieldsObj, this.tracker, this.opts.hitFilter));
});
}

/**
* Handles `unlike` events emitted by the Facebook JS SDK.
* @param {string} url The URL corresponding to the unlike event.
*/
handleUnlikeEvents(url) {
this.queue.pushTask(({time}) => {
/** @type {FieldsObj} */
const defaultFields = {
transport: 'beacon',
socialNetwork: 'Facebook',
socialAction: 'unlike',
socialTarget: url,
queueTime: now() - time,
};
this.tracker.send('social', createFieldsObj(defaultFields,
this.opts.fieldsObj, this.tracker, this.opts.hitFilter));
});
handleFacebookEvents(event) {
/** @type {HTMLElement} */ let iframe = document.activeElement;
if (iframe && iframe.tagName == 'IFRAME') {
if (iframe.src.indexOf('https://www.facebook.com/') == 0) {
// Possible iframe titles:
// "fb:like Facebook Social Plugin"
// "fb:share_button Facebook Social Plugin"
let action = iframe.title.split(' ')[0];
if (action.indexOf('fb:') == 0) {
action = action.slice(3).split('_')[0];

this.queue.pushTask(({time}) => {
/** @type {FieldsObj} */ const defaultFields = {
transport: 'beacon',
socialNetwork: 'Facebook',
socialAction: action,
socialTarget: location.href,
queueTime: now() - time,
};
this.tracker.send('social', createFieldsObj(defaultFields,
this.opts.fieldsObj, this.tracker, this.opts.hitFilter));
});
}
}
}
}

/**
Expand Down