Skip to content

Commit

Permalink
adding track events
Browse files Browse the repository at this point in the history
  • Loading branch information
jparez committed Jul 18, 2024
1 parent 5ee9a2b commit be69843
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
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 TRACKEVENT change
this.instance.store.subscribe(
({trackedEvents}) => {
if (this.instance.store.state.trackedEvents.events.length) {
cb(trackedEvents);
// 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 @@ -126,6 +126,14 @@ class OverlayPlugin {
toOpen: this.widget.classList.contains('gm-hidden'),
},
});
this.instance.store.dispatch({
type: 'ADD_TRACKED_EVENT',
payload: {
category: 'widget',
action: this.widget.classList.contains('gm-hidden') ? 'open' : 'close',
name: this.constructor.name,
},
});
}

clickHandlerCloseOverlay(event) {
Expand Down
17 changes: 17 additions & 0 deletions 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 @@ -131,6 +135,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

0 comments on commit be69843

Please sign in to comment.