Skip to content

Commit

Permalink
ia analytics, move to 1% sampled bucket (#1105)
Browse files Browse the repository at this point in the history
  • Loading branch information
iisa authored Nov 17, 2022
1 parent 1947d88 commit 302c3d3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
2 changes: 2 additions & 0 deletions BookReaderDemo/IADemoBr.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ const initializeBookReader = (brManifest) => {
// we expect this at the global level
BookReaderJSIAinit(brManifest.data, options);

const isRestricted = brManifest.data.isRestricted;
window.dispatchEvent(new CustomEvent('contextmenu', { detail: { isRestricted } }));
if (customAutoflipParams.autoflip) {
br.autoToggle(customAutoflipParams);
}
Expand Down
4 changes: 2 additions & 2 deletions src/BookNavigator/book-navigator.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,10 +449,10 @@ export class BookNavigator extends LitElement {
/** Display an element's context menu */
manageContextMenuVisibility(e) {
if (window.archive_analytics) {
window.archive_analytics?.send_event_no_sampling(
window.archive_analytics?.send_event(
'BookReader',
`contextmenu-${this.bookIsRestricted ? 'restricted' : 'unrestricted'}`,
e.target.classList.value
e.target?.classList?.value
);
}
if (!this.bookIsRestricted) {
Expand Down
2 changes: 1 addition & 1 deletion src/BookNavigator/volumes/volumes-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export default class VolumesProvider {
if (!window.archive_analytics) {
return;
}
window.archive_analytics?.send_event_no_sampling(
window.archive_analytics?.send_event(
'BookReader',
`VolumesSort|${orderBy}`,
window.location.path,
Expand Down
34 changes: 19 additions & 15 deletions tests/jest/BookNavigator/book-navigator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ window.ResizeObserver = class ResizeObserver {
disconnect = sinon.fake()
};

beforeEach(() => {
window.archive_analytics = {
send_event_no_sampling: sinon.fake(),
send_event: sinon.fake()
};
});

afterEach(() => {
window.br = null;
fixtureCleanup();
Expand Down Expand Up @@ -516,8 +523,6 @@ describe('<book-navigator>', () => {
describe('Handles Restricted Books', () => {
describe('contextMenu is prevented when book is restricted', () => {
it('watches on `div.BRscreen`', async () => {
window.archive_analytics = { send_event_no_sampling: sinon.fake() };

const el = fixtureSync(container());
const brStub = {
options: { restricted: true },
Expand All @@ -529,9 +534,8 @@ describe('<book-navigator>', () => {
const elSpy = sinon.spy(el.manageContextMenuVisibility);
await el.elementUpdated;

expect(window.archive_analytics.send_event_no_sampling.called).toEqual(
false
);
expect(window.archive_analytics.send_event_no_sampling.called).toEqual(false);
expect(window.archive_analytics.send_event.called).toEqual(false);
expect(elSpy.called).toEqual(false);

const body = document.querySelector('body');
Expand All @@ -550,14 +554,13 @@ describe('<book-navigator>', () => {

// analytics fires
expect(window.archive_analytics.send_event_no_sampling.called).toEqual(
true
false
);
expect(window.archive_analytics.send_event.called).toEqual(true);
// we prevent default
expect(preventDefaultSpy.called).toEqual(true);
});
it('watches on `img.BRpageimage`', async () => {
window.archive_analytics = { send_event_no_sampling: sinon.fake() };

const el = fixtureSync(container());
const brStub = {
options: { restricted: true },
Expand All @@ -568,9 +571,8 @@ describe('<book-navigator>', () => {

await el.elementUpdated;

expect(window.archive_analytics.send_event_no_sampling.called).toEqual(
false
);
expect(window.archive_analytics.send_event_no_sampling.called).toEqual(false);
expect(window.archive_analytics.send_event.called).toEqual(false);

const body = document.querySelector('body');
// const element stub for img.BRpageimage
Expand All @@ -586,14 +588,13 @@ describe('<book-navigator>', () => {
imgBRpageimage.dispatchEvent(contextMenuEvent);

// analytics fires
expect(window.archive_analytics.send_event_no_sampling.called).toEqual(true);
expect(window.archive_analytics.send_event_no_sampling.called).toEqual(false);
expect(window.archive_analytics.send_event.called).toEqual(true);
// we prevent default
expect(preventDefaultSpy.called).toEqual(true);
});
});
it('Allows unrestricted books access to context menu', async () => {
window.archive_analytics = { send_event_no_sampling: sinon.fake() };

const el = fixtureSync(container());
const brStub = {
options: { restricted: false },
Expand All @@ -607,6 +608,8 @@ describe('<book-navigator>', () => {
expect(window.archive_analytics.send_event_no_sampling.called).toEqual(
false
);
expect(window.archive_analytics.send_event.called).toEqual(false);


const body = document.querySelector('body');
// const element stub for img.BRpageimage
Expand All @@ -622,7 +625,8 @@ describe('<book-navigator>', () => {
imgBRpageimage.dispatchEvent(contextMenuEvent);

// analytics fires
expect(window.archive_analytics.send_event_no_sampling.called).toEqual(true);
expect(window.archive_analytics.send_event_no_sampling.called).toEqual(false);
expect(window.archive_analytics.send_event.called).toEqual(true);
// we do not prevent default
expect(preventDefaultSpy.called).toEqual(false);
});
Expand Down

0 comments on commit 302c3d3

Please sign in to comment.