Skip to content

Commit

Permalink
adding feature to trackEvents from outside of webplayer
Browse files Browse the repository at this point in the history
  • Loading branch information
jparez committed Jul 24, 2024
1 parent 5ee9a2b commit 31b2790
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 1 deletion.
31 changes: 31 additions & 0 deletions src/APIManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,37 @@ module.exports = class APIManager {
},
description: 'disconnect from the instance',
});

this.registerFunction({
name: 'enableTrackEvents',
category: 'analytics',
fn: (isActive) => {
this.instance.store.dispatch({type: 'ENABLE_TRACKED_EVENTS', payload: isActive});
if (!isActive) {
this.instance.store.dispatch({type: 'FLUSH_TRACKED_EVENTS'});
}
},
description: 'call a callback with an array of tracked events',
});

this.registerFunction({
name: 'trackEvents',
category: 'analytics',
fn: (cb) => {
// subcribe to store's TRACKEVENT change
this.instance.store.subscribe(
({trackedEvents}) => {
if (this.instance.store.state.trackedEvents.events.length) {
cb([...trackedEvents.events]);
// flush the trackedEvents
this.instance.store.dispatch({type: 'FLUSH_TRACKED_EVENTS'});
}
},
['trackedEvents.events'],
);
},
description: 'call a callback with an array of tracked events',
});
}

registerFunction({name, category = 'global', fn, description = ''}) {
Expand Down
8 changes: 8 additions & 0 deletions src/plugins/FileUpload.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,14 @@ module.exports = class FileUpload extends OverlayPlugin {
textProgress.innerHTML = this.i18n.UPLOADER_INSTALLING || 'Installing...';
} else if (data[0] === 'ready' && data.length >= 2) {
if (data[1].indexOf('opengapps') !== -1) {
this.instance.store.dispatch({
type: 'ADD_TRACKED_EVENT',
payload: {
category: 'opengapps',
type: 'installed',
name: 'true',
},
});
this.updateOpenGAppsStatus(true);
}

Expand Down
14 changes: 14 additions & 0 deletions src/plugins/GamepadManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,13 @@ module.exports = class GamepadManager {
* @param {GamepadEvent} event raw event coming from the browser Gamepad API
*/
onGamepadConnected(event) {
this.instance.store.dispatch({
type: 'ADD_TRACKED_EVENT',
payload: {
category: 'gamepad',
action: 'plugged',
},
});
const customEvent = new CustomEvent('gm-gamepadConnected', {detail: this.parseGamepad(event.gamepad)});
window.dispatchEvent(customEvent);
}
Expand All @@ -271,6 +278,13 @@ module.exports = class GamepadManager {
* @param {GamepadEvent} event raw event coming from the browser Gamepad API
*/
onGamepadDisconnected(event) {
this.instance.store.dispatch({
type: 'ADD_TRACKED_EVENT',
payload: {
category: 'gamepad',
action: 'unplugged',
},
});
const customEvent = new CustomEvent('gm-gamepadDisconnected', {detail: this.parseGamepad(event.gamepad)});
window.dispatchEvent(customEvent);
this.stopListeningInputs(event.gamepad.index);
Expand Down
8 changes: 8 additions & 0 deletions src/plugins/util/OverlayPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ class OverlayPlugin {
if (this.toolbarBtnImage) {
this.toolbarBtnImage.classList.add('gm-active');
}
this.instance.store.dispatch({
type: 'ADD_TRACKED_EVENT',
payload: {
category: 'widget',
action: 'open',
name: this.constructor.name,
},
});
}

/**
Expand Down
20 changes: 19 additions & 1 deletion src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ const initialState = {
},
isKeyboardEventsEnabled: false,
isMouseEventsEnabled: false,
trackedEvents: {
isActive: false,
events: [],
},
};

const createStore = (instance, reducer) => {
Expand Down Expand Up @@ -68,7 +72,8 @@ const createStore = (instance, reducer) => {
const notifyListeners = (changedKeys) => {
listeners.forEach(({keys, cb}) => {
if (keys.length === 0 || keys.some((key) => hasChanged(changedKeys, key))) {
cb(instance.store.state);
// send a copy of the store's state, in order to avoid mutation of the store
cb({...instance.store.state});
}
});
};
Expand Down Expand Up @@ -131,6 +136,19 @@ const reducer = (state, action) => {
state.isMouseEventsEnabled = true;
}
break;
case 'ENABLE_TRACKED_EVENTS':
state.trackedEvents.isActive = action.payload;
break;
case 'ADD_TRACKED_EVENT':
if (!state.trackedEvents.isActive) {
return state;
}
state.trackedEvents.events.push(action.payload);

break;
case 'FLUSH_TRACKED_EVENTS':
state.trackedEvents.events.length = 0;
break;
default:
log.debug('Store not updated, action type :', action.type, ' unknown');
break;
Expand Down
57 changes: 57 additions & 0 deletions tests/unit/apiManager.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use strict';

const ApiManager = require('../../src/APIManager');
const Instance = require('../mocks/DeviceRenderer');
const Clipboard = require('../../src/plugins/Clipboard');
const store = require('../../src/store/index');

let apiManager;
let instance;
let exposedApiFunctions;

describe('APIManager', () => {
beforeEach(() => {
instance = new Instance({});
apiManager = new ApiManager(instance);
exposedApiFunctions = apiManager.getExposedApiFunctions();
store(instance);

new Clipboard(instance, {
CLIPBOARD_TITLE: 'TEST CLIPBOARD PLUGIN TITLE',
});
});

describe('has exposed api', () => {
test('getRegisteredFunctions', () => {
const registeredFunctions = exposedApiFunctions.utils.getRegisteredFunctions();
expect(Object.keys(registeredFunctions)).toEqual(
expect.arrayContaining([
'sendData',
'getRegisteredFunctions',
'addEventListener',
'disconnect',
'enableTrackEvents',
'trackEvents',
]),
);
});

test('sendTrackEvent', () => {
let events = [];

exposedApiFunctions.analytics.enableTrackEvents(true);

// attach callback to get events
exposedApiFunctions.analytics.trackEvents((evts) => {
events = evts;
});

const button = document.getElementsByClassName('gm-clipboard-button')[0];
expect(button).toBeTruthy();
button.click();

// expect object to be exactly the same
expect(events).toEqual([{category: 'widget', action: 'open', name: 'Clipboard'}]);
});
});
});

0 comments on commit 31b2790

Please sign in to comment.