-
Notifications
You must be signed in to change notification settings - Fork 428
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1371 from cdrini/refactor/br-plugin
Introduce BookReaderPlugin abstraction and apply to ArchiveAnalyticsPlugin
- Loading branch information
Showing
7 changed files
with
175 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// @ts-check | ||
/** @typedef {import("./BookReader.js").default} BookReader */ | ||
|
||
/** | ||
* @template TOptions | ||
*/ | ||
export class BookReaderPlugin { | ||
/** | ||
* @param {BookReader} br | ||
*/ | ||
constructor(br) { | ||
/** @type {BookReader} */ | ||
this.br = br; | ||
/** @type {TOptions} */ | ||
this.options; | ||
} | ||
|
||
/** | ||
* @abstract | ||
* @param {TOptions} options | ||
**/ | ||
setup(options) { | ||
this.options = Object.assign({}, this.options, options); | ||
} | ||
|
||
/** @abstract */ | ||
init() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,92 @@ | ||
/* global BookReader */ | ||
/** | ||
* Plugin for Archive.org analytics | ||
*/ | ||
jQuery.extend(BookReader.defaultOptions, { | ||
enableArchiveAnalytics: true, | ||
/** Provide a means of debugging, cause otherwise it's impossible to test locally */ | ||
debugArchiveAnaltyics: false, | ||
}); | ||
|
||
BookReader.prototype.init = (function(super_) { | ||
return function() { | ||
super_.call(this); | ||
|
||
if (this.options.enableArchiveAnalytics) { | ||
this.bind(BookReader.eventNames.fragmentChange, () => this.archiveAnalyticsSendFragmentChange()); | ||
} | ||
}; | ||
})(BookReader.prototype.init); | ||
// @ts-check | ||
import { BookReaderPlugin } from "../BookReaderPlugin.js"; | ||
|
||
const BookReader = /** @type {typeof import('../BookReader').default} */(window.BookReader); | ||
|
||
|
||
/** @private */ | ||
BookReader.prototype.archiveAnalyticsSendFragmentChange = function() { | ||
if (!window.archive_analytics) { | ||
return; | ||
export class ArchiveAnalyticsPlugin extends BookReaderPlugin { | ||
options = { | ||
enabled: true, | ||
/** Provide a means of debugging, cause otherwise it's impossible to test locally */ | ||
debug: false, | ||
} | ||
|
||
const prevFragment = this.archiveAnalyticsSendFragmentChange.prevFragment; | ||
|
||
const params = this.paramsFromCurrent(); | ||
const newFragment = this.fragmentFromParams(params); | ||
|
||
if (prevFragment != newFragment) { | ||
const values = { | ||
bookreader: "user_changed_view", | ||
itemid: this.bookId, | ||
cache_bust: Math.random(), | ||
}; | ||
// EEK! offsite embedding and /details/ page books look the same in analytics, otherwise! | ||
values.offsite = 1; | ||
values.details = 0; | ||
try { | ||
values.offsite = window.top.location.hostname.match(/\.archive.org$/) | ||
? 0 | ||
: 1; | ||
values.details = | ||
!values.offsite && window.top.location.pathname.match(/^\/details\//) | ||
? 1 | ||
: 0; | ||
} catch (e) {} | ||
// avoids embed cross site exceptions -- but on (+) side, means it is and keeps marked offite! | ||
|
||
// Send bookreader ping | ||
window.archive_analytics.send_ping(values, null, "augment_for_ao_site"); | ||
|
||
// Also send tracking event ping | ||
const additionalEventParams = this.options.lendingInfo && this.options.lendingInfo.loanId | ||
? { loanId: this.options.lendingInfo.loanId } | ||
: {}; | ||
window.archive_analytics.send_event('BookReader', 'UserChangedView', window.location.pathname, additionalEventParams); | ||
|
||
this.archiveAnalyticsSendFragmentChange.prevFragment = newFragment; | ||
/** @type {string} */ | ||
_prevFragment = null; | ||
|
||
/** @override */ | ||
init() { | ||
if (this.options.enabled) { | ||
this.br.bind(BookReader.eventNames.fragmentChange, () => this.sendFragmentChange()); | ||
} | ||
} | ||
}; | ||
|
||
/** | ||
* Sends a tracking "Event". See https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#events | ||
* @param {string} category | ||
* @param {string} action | ||
* @param {number} [value] (must be an int) | ||
* @param {Object} [additionalEventParams] | ||
*/ | ||
BookReader.prototype.archiveAnalyticsSendEvent = function(category, action, value, additionalEventParams) { | ||
if (!this.options.enableArchiveAnalytics) return; | ||
|
||
if (this.options.debugArchiveAnaltyics) { | ||
console.log("archiveAnalyticsSendEvent", arguments, window.archive_analytics); | ||
|
||
/** @private */ | ||
sendFragmentChange() { | ||
if (!window.archive_analytics) { | ||
return; | ||
} | ||
|
||
const prevFragment = this._prevFragment; | ||
|
||
const params = this.br.paramsFromCurrent(); | ||
const newFragment = this.br.fragmentFromParams(params); | ||
|
||
if (prevFragment != newFragment) { | ||
const values = { | ||
bookreader: "user_changed_view", | ||
itemid: this.br.bookId, | ||
cache_bust: Math.random(), | ||
}; | ||
// EEK! offsite embedding and /details/ page books look the same in analytics, otherwise! | ||
values.offsite = 1; | ||
values.details = 0; | ||
try { | ||
values.offsite = window.top.location.hostname.match(/\.archive.org$/) | ||
? 0 | ||
: 1; | ||
values.details = | ||
!values.offsite && window.top.location.pathname.match(/^\/details\//) | ||
? 1 | ||
: 0; | ||
} catch (e) { } | ||
// avoids embed cross site exceptions -- but on (+) side, means it is and keeps marked offite! | ||
|
||
// Send bookreader ping | ||
window.archive_analytics.send_ping(values, null, "augment_for_ao_site"); | ||
|
||
// Also send tracking event ping | ||
const additionalEventParams = this.br.options.lendingInfo?.loanId | ||
? { loanId: this.br.options.lendingInfo.loanId } | ||
: {}; | ||
window.archive_analytics.send_event('BookReader', 'UserChangedView', window.location.pathname, additionalEventParams); | ||
|
||
this._prevFragment = newFragment; | ||
} | ||
} | ||
|
||
if (!window.archive_analytics) return; | ||
/** | ||
* Sends a tracking "Event". See https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#events | ||
* @param {string} category | ||
* @param {string} action | ||
* @param {number} [value] (must be an int) | ||
* @param {Object} [additionalEventParams] | ||
*/ | ||
sendEvent(category, action, value, additionalEventParams) { | ||
if (!this.options.enabled) return; | ||
|
||
additionalEventParams = additionalEventParams || {}; | ||
if (typeof(value) == 'number') { | ||
additionalEventParams.ev = value; | ||
if (this.options.debug) { | ||
console.log("archiveAnalyticsSendEvent", arguments, window.archive_analytics); | ||
} | ||
|
||
if (!window.archive_analytics) return; | ||
|
||
additionalEventParams = additionalEventParams || {}; | ||
if (typeof (value) == 'number') { | ||
additionalEventParams.ev = value; | ||
} | ||
window.archive_analytics.send_event(category, action, null, additionalEventParams); | ||
} | ||
window.archive_analytics.send_event(category, action, null, additionalEventParams); | ||
}; | ||
} | ||
|
||
BookReader?.registerPlugin('archiveAnalytics', ArchiveAnalyticsPlugin); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,20 @@ | ||
import sinon from 'sinon'; | ||
import BookReader from '@/src/BookReader.js'; | ||
import '@/src/plugins/plugin.archive_analytics.js'; | ||
|
||
describe('archiveAnalyticsSendEvent', () => { | ||
const sendEvent = BookReader.prototype.archiveAnalyticsSendEvent; | ||
import {ArchiveAnalyticsPlugin} from '@/src/plugins/plugin.archive_analytics.js'; | ||
|
||
describe('sendEvent', () => { | ||
test('logs if debug set to true', () => { | ||
const stub = sinon.stub(console, 'log'); | ||
const FAKE_BR = { options: { enableArchiveAnalytics: true, debugArchiveAnaltyics: true }}; | ||
sendEvent.call(FAKE_BR); | ||
const p = new ArchiveAnalyticsPlugin({}); | ||
p.setup({ debug: true }); | ||
p.sendEvent(); | ||
expect(stub.callCount).toBe(1); | ||
stub.restore(); | ||
}); | ||
|
||
test('Does not error if window.archive_analytics is undefined', () => { | ||
const spy = sinon.spy(sendEvent); | ||
const FAKE_BR = { options: { enableArchiveAnalytics: true }}; | ||
spy.call(FAKE_BR); | ||
const p = new ArchiveAnalyticsPlugin({}); | ||
const spy = sinon.spy(p.sendEvent); | ||
p.sendEvent(); | ||
expect(spy.threw()).toBe(false); | ||
}); | ||
}); | ||
|