Skip to content

Commit

Permalink
Update socialWidgetTracker to use TrackerQueue
Browse files Browse the repository at this point in the history
  • Loading branch information
philipwalton committed Aug 18, 2018
1 parent 220bd71 commit ed04094
Showing 1 changed file with 72 additions and 54 deletions.
126 changes: 72 additions & 54 deletions lib/plugins/social-widget-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@


import provide from '../provide';
import TrackerQueue from '../tracker-queue';
import {plugins, trackUsage} from '../usage';
import {assign, createFieldsObj} from '../utilities';
import {assign, createFieldsObj, now} from '../utilities';


/**
Expand Down Expand Up @@ -57,6 +58,8 @@ class SocialWidgetTracker {
this.handleLikeEvents = this.handleLikeEvents.bind(this);
this.handleUnlikeEvents = this.handleUnlikeEvents.bind(this);

this.queue = TrackerQueue.getOrCreate(tracker);

if (document.readyState != 'complete') {
// Adds the widget listeners after the window's `load` event fires.
// If loading widgets using the officially recommended snippets, they
Expand All @@ -74,8 +77,10 @@ class SocialWidgetTracker {
* Ensures the respective global namespaces are present before adding.
*/
addWidgetListeners() {
if (window.FB) this.addFacebookEventHandlers();
if (window.twttr) this.addTwitterEventHandlers();
this.queue.add(() => {
if (window.FB) this.addFacebookEventHandlers();
if (window.twttr) this.addTwitterEventHandlers();
});
}

/**
Expand Down Expand Up @@ -140,86 +145,99 @@ class SocialWidgetTracker {
* @param {TwttrEvent} event The Twitter event object passed to the handler.
*/
handleTweetEvents(event) {
// Ignores tweets from widgets that aren't the tweet button.
if (event.region != 'tweet') return;

const url = event.data.url || event.target.getAttribute('data-url') ||
location.href;

/** @type {FieldsObj} */
const defaultFields = {
transport: 'beacon',
socialNetwork: 'Twitter',
socialAction: 'tweet',
socialTarget: url,
};
this.tracker.send('social',
createFieldsObj(defaultFields, this.opts.fieldsObj,
this.tracker, this.opts.hitFilter, event.target, event));
this.queue.add(({time}) => {
// Ignores tweets from widgets that aren't the tweet button.
if (event.region != 'tweet') return;

const url = event.data.url || event.target.getAttribute('data-url') ||
location.href;

/** @type {FieldsObj} */
const defaultFields = {
transport: 'beacon',
socialNetwork: 'Twitter',
socialAction: 'tweet',
socialTarget: url,
queueTime: now() - time,
};
this.tracker.send('social',
createFieldsObj(defaultFields, this.opts.fieldsObj,
this.tracker, this.opts.hitFilter, event.target, event));
});
}

/**
* Handles `follow` events emitted by the Twitter JS SDK.
* @param {TwttrEvent} event The Twitter event object passed to the handler.
*/
handleFollowEvents(event) {
// Ignore follows from widgets that aren't the follow button.
if (event.region != 'follow') return;

const screenName = event.data.screen_name ||
event.target.getAttribute('data-screen-name');

/** @type {FieldsObj} */
const defaultFields = {
transport: 'beacon',
socialNetwork: 'Twitter',
socialAction: 'follow',
socialTarget: screenName,
};
this.tracker.send('social',
createFieldsObj(defaultFields, this.opts.fieldsObj,
this.tracker, this.opts.hitFilter, event.target, event));
this.queue.add(({time}) => {
// Ignore follows from widgets that aren't the follow button.
if (event.region != 'follow') return;

const screenName = event.data.screen_name ||
event.target.getAttribute('data-screen-name');

/** @type {FieldsObj} */
const defaultFields = {
transport: 'beacon',
socialNetwork: 'Twitter',
socialAction: 'follow',
socialTarget: screenName,
queueTime: now() - time,
};
this.tracker.send('social',
createFieldsObj(defaultFields, this.opts.fieldsObj,
this.tracker, this.opts.hitFilter, event.target, event));
});
}

/**
* Handles `like` events emitted by the Facebook JS SDK.
* @param {string} url The URL corresponding to the like event.
*/
handleLikeEvents(url) {
/** @type {FieldsObj} */
const defaultFields = {
transport: 'beacon',
socialNetwork: 'Facebook',
socialAction: 'like',
socialTarget: url,
};
this.tracker.send('social', createFieldsObj(defaultFields,
this.opts.fieldsObj, this.tracker, this.opts.hitFilter));
this.queue.add(({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) {
/** @type {FieldsObj} */
const defaultFields = {
transport: 'beacon',
socialNetwork: 'Facebook',
socialAction: 'unlike',
socialTarget: url,
};
this.tracker.send('social', createFieldsObj(defaultFields,
this.opts.fieldsObj, this.tracker, this.opts.hitFilter));
this.queue.add(({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));
});
}

/**
* Removes all event listeners and instance properties.
*/
remove() {
window.removeEventListener('load', this.addWidgetListeners);
this.queue.destroy();
this.removeFacebookEventHandlers();
this.removeTwitterEventHandlers();
window.removeEventListener('load', this.addWidgetListeners);
}
}

Expand Down

0 comments on commit ed04094

Please sign in to comment.