Skip to content

Commit

Permalink
tracking now moved to a module
Browse files Browse the repository at this point in the history
  • Loading branch information
Mattk70 committed Mar 9, 2024
1 parent 74a98e0 commit 89ece9c
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 94 deletions.
15 changes: 8 additions & 7 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,11 @@ <h5 class="offcanvas-title" id="offcanvasExampleLabel">Settings</h5>
<div class="form-check form-check-inline">
<label class="form-check-label" for="tensorflow">CPU</label>
<input class="form-check-input" type="radio" name="backend" id="tensorflow" value="tensorflow" checked="">

</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="backend" id="webgl" value="webgl">
<label class="form-check-label" for="webgl">GPU</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="backend" id="webgpu" value="webgpu">
<label class="form-check-label" for="webgpu">webGPU</label>
</div>
</div>
</div>
</div>
Expand Down Expand Up @@ -1160,8 +1155,9 @@ <h5 class="modal-title" id="locationModalLabel">Set Location</h5>
<script src="node_modules/wavesurfer.js/dist/plugin/wavesurfer.spectrogram.min.js"></script>
<script src="node_modules/wavesurfer.js/dist/plugin/wavesurfer.timeline.min.js"></script>
<script src="node_modules/wavesurfer.js/dist/plugin/wavesurfer.regions.min.js"></script>
<script defer src="./js/ui.js"></script>
<script>
<script src="./js/ui.js" type="module"></script>
<script type="module" defer>
import { config, displayLocationAddress, LOCATIONS } from './js/ui.js';
let map, marker;
async function onMapClick(e) {
const {lat, lng} = e.latlng;
Expand Down Expand Up @@ -1242,6 +1238,7 @@ <h5 class="modal-title" id="locationModalLabel">Set Location</h5>
async function placeMap(divID){
map?.remove(); marker = undefined;
map = L.map(divID);
let latTxt, lonTxt;
if (divID === 'settingsMap'){
latTxt = 'latitude';
lonTxt = 'longitude';
Expand All @@ -1254,6 +1251,10 @@ <h5 class="modal-title" id="locationModalLabel">Set Location</h5>
showMap(divID);
map.on('click', onMapClick);
}

window.placeMap = placeMap;
window.updateMap = updateMap;
window.map = map;
</script>
</body>

Expand Down
1 change: 0 additions & 1 deletion js/model.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const tf = require('@tensorflow/tfjs-node');
require('@tensorflow/tfjs-backend-webgpu');
const fs = require('node:fs');
const path = require('node:path');
let DEBUG = false;
Expand Down
3 changes: 2 additions & 1 deletion js/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ export class State {
this.customList = undefined,
this.local = true,
this.incrementor = 1,
this.UUID = 0
this.UUID = 0,
this.track = true
}


Expand Down
39 changes: 39 additions & 0 deletions js/tracking.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const DEBUG = true;
const ID_SITE = 3;


function trackEvent(uuid, event, action, name, value){
DEBUG && event === 'Error' && console.log(action, name);
const t = new Date()
name = name ? `&e_n=${name}` : '';
value = value ? `&e_v=${value}` : '';
fetch(`https://analytics.mattkirkland.co.uk/matomo.php?h=${t.getHours()}&m=${t.getMinutes()}&s=${t.getSeconds()}
&action_name=Settings%20Change&idsite=${ID_SITE}&rand=${Date.now()}&rec=1&uid=${uuid}&apiv=1
&e_c=${event}&e_a=${action}${name}${value}`)
.then(response => {
if (! response.ok) throw new Error('Network response was not ok', response);
})
.catch(error => console.log('Error posting tracking:', error))
}

function trackVisit(config){
const {width, height} = window.screen;
fetch(`https://analytics.mattkirkland.co.uk/matomo.php?idsite=${ID_SITE}&rand=${Date.now()}&rec=1&uid=${config.UUID}&apiv=1
&res=${width}x${height}
&dimension1=${config.model}
&dimension2=${config.list}
&dimension3=${config.useWeek}
&dimension4=${config.locale}
&dimension5=${config.speciesThreshold}
&dimension6=${JSON.stringify(config.filters)}
&dimension7=${JSON.stringify(config.audio)}
&dimension8=${JSON.stringify(config[config.backend])}
&dimension9=${JSON.stringify(config.detect)}
&dimension11=${config.VERSION}`)
.then(response => {
if (! response.ok) throw new Error('Network response was not ok', response);
})
.catch(error => console.log('Error posting tracking:', error))
}

export {trackEvent, trackVisit}
75 changes: 21 additions & 54 deletions js/ui.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const ID_SITE = 2;

import {trackVisit, trackEvent} from './tracking.js';

let seenTheDarkness = false, shownDaylightBanner = false, LOCATIONS, locationID = undefined;
const startTime = performance.now();
Expand All @@ -23,7 +22,7 @@ console.warn = function(message) {
originalWarn.apply(console, arguments);

// Track the warning message using your tracking function
track('Warnings', arguments[0], customURLEncode(arguments[1]));
config.track && trackEvent(config.UUID, 'Warnings', arguments[0], customURLEncode(arguments[1]));
};

// Override console.error to intercept and track errors
Expand All @@ -32,7 +31,7 @@ console.error = function(message) {
originalError.apply(console, arguments);

// Track the error message using your tracking function
track('Errors', arguments[0], customURLEncode(arguments[1]));
config.track && trackEvent(config.UUID, 'Errors', arguments[0], customURLEncode(arguments[1]));
};


Expand All @@ -42,7 +41,7 @@ window.addEventListener('unhandledrejection', function(event) {
const stackTrace = event.reason.stack;

// Track the unhandled promise rejection
track('Unhandled UI Promise Rejection', errorMessage, customURLEncode(stackTrace));
config.track && trackEvent(config.UUID, 'Unhandled UI Promise Rejection', errorMessage, customURLEncode(stackTrace));
});

window.addEventListener('rejectionhandled', function(event) {
Expand All @@ -51,7 +50,7 @@ window.addEventListener('rejectionhandled', function(event) {
const stackTrace = event.reason.stack;

// Track the unhandled promise rejection
track('Handled UI Promise Rejection', errorMessage, customURLEncode(stackTrace));
config.track && trackEvent(config.UUID, 'Handled UI Promise Rejection', errorMessage, customURLEncode(stackTrace));
});

const STATE = {
Expand Down Expand Up @@ -685,7 +684,7 @@ const showLocation = async (fromSelect) => {

const displayLocationAddress = async (where) => {
const custom = where.includes('custom');
let latEl, lonEl, placeEl;
let latEl, lonEl, placeEl, address;
if (custom){
latEl = document.getElementById('customLat');
lonEl = document.getElementById('customLon');
Expand Down Expand Up @@ -1453,7 +1452,8 @@ window.onload = async () => {
audio: { gain: 0, format: 'mp3', bitrate: 192, quality: 5, downmix: false, padding: false, fade: false, notification: true, normalise: false },
limit: 500,
track: true,
debug: false
debug: false,
VERSION: VERSION
};
// Load preferences and override defaults
[appPath, tempPath] = await getPaths();
Expand All @@ -1474,7 +1474,7 @@ window.onload = async () => {

// Attach an error event listener to the window object
window.onerror = function(message, file, lineno, colno, error) {
track('Error', error.message, encodeURIComponent(error.stack));
config.track && trackEvent(config.UUID, 'Error', error.message, encodeURIComponent(error.stack));
// Return false not to inhibit the default error handling
return false;
};
Expand Down Expand Up @@ -1618,25 +1618,7 @@ window.onload = async () => {
isMac && checkForMacUpdates();
const doNotTrack = document.getElementById('do-not-track')
doNotTrack.checked = !config.track;
if (config.track) {
const {width, height} = window.screen;
fetch(`https://analytics.mattkirkland.co.uk/matomo.php?idsite=${ID_SITE}&rand=${Date.now()}&rec=1&uid=${config.UUID}&apiv=1
&res=${width}x${height}
&dimension1=${config.model}
&dimension2=${config.list}
&dimension3=${config.useWeek}
&dimension4=${config.locale}
&dimension5=${config.speciesThreshold}
&dimension6=${JSON.stringify(config.filters)}
&dimension7=${JSON.stringify(config.audio)}
&dimension8=${JSON.stringify(config[config.backend])}
&dimension9=${JSON.stringify(config.detect)}
&dimension11=${VERSION}`)
.then(response => {
if (! response.ok) throw new Error('Network response was not ok', response);
})
.catch(error => console.log('Error posting tracking:', error))
}
if (config.track) trackVisit(config);
})
}

Expand All @@ -1662,10 +1644,6 @@ const setUpWorkerMessaging = () => {
exploreLink.classList.remove("disabled");
break;
}
case 'error': {
track('Error', args.message, args.source, args.value);
break;
}
case "file-location-id": {onFileLocationID(args);
break;
}
Expand Down Expand Up @@ -2933,9 +2911,9 @@ function onChartData(args) {
DIAGNOSTICS['Analysis Duration'] = analysisTime + ' seconds';
const rate = (DIAGNOSTICS['Audio Duration'] / analysisTime);
DIAGNOSTICS['Analysis Rate'] = rate.toFixed(0) + 'x faster than real time performance.';
track(`${config.model}-${config.backend}`, 'Audio Duration', config.backend, Math.round(DIAGNOSTICS['Audio Duration']));
track(`${config.model}-${config.backend}`, 'Analysis Duration', config.backend, parseInt(analysisTime));
track(`${config.model}-${config.backend}`, 'Analysis Rate', config.backend, parseInt(rate));
trackEvent(config.UUID, `${config.model}-${config.backend}`, 'Audio Duration', config.backend, Math.round(DIAGNOSTICS['Audio Duration']));
trackEvent(config.UUID, `${config.model}-${config.backend}`, 'Analysis Duration', config.backend, parseInt(analysisTime));
trackEvent(config.UUID, `${config.model}-${config.backend}`, 'Analysis Rate', config.backend, parseInt(rate));
generateToast({ message:'Analysis complete.'})
}

Expand Down Expand Up @@ -3653,7 +3631,7 @@ function onChartData(args) {
filelist.push(f.path);
}
if (filelist.length) filterValidFiles({ filePaths: filelist })
track('UI', 'Drop', 'Open Folder(s)', filelist.length);
trackEvent(config.UUID, 'UI', 'Drop', 'Open Folder(s)', filelist.length);
});


Expand Down Expand Up @@ -4139,7 +4117,7 @@ DOM.gain.addEventListener('input', () => {
contextMenu.classList.add("d-none");
hideConfidenceSlider();
config.debug && console.log('clicked', target);
target && target !== 'result1' && track('UI', 'Click', target);
target && target !== 'result1' && trackEvent(config.UUID, 'UI', 'Click', target);
})


Expand Down Expand Up @@ -4186,6 +4164,7 @@ DOM.gain.addEventListener('input', () => {
}
case 'do-not-track': {
config.track = !element.checked;
worker.postMessage({ action: 'update-state', track: config.track })
break;
}
case 'lowShelfFrequency': {
Expand Down Expand Up @@ -4358,7 +4337,7 @@ DOM.gain.addEventListener('input', () => {
}
updatePrefs();
const value = element.type === "checkbox" ? element.checked : element.value;
track('Settings Change', target, value);
trackEvent(config.UUID, 'Settings Change', target, value);
}
})

Expand Down Expand Up @@ -4391,7 +4370,7 @@ function setListUIState(list){

if (updating === 'list'){
worker.postMessage({ action: 'update-list', list: config.list, customList: LABELS, refreshResults: STATE.analysisDone});
track('UI', 'Create', 'Custom list', LABELS.length)
trackEvent(config.UUID, 'UI', 'Create', 'Custom list', LABELS.length)
} else {
worker.postMessage({action: 'update-locale', locale: config[config.model].locale, labels: LABELS, refreshResults: STATE.analysisDone})
}
Expand All @@ -4404,21 +4383,7 @@ function setListUIState(list){
else {console.error('There was a problem reading the label file:', error);}
})
}
function track(event, action, name, value){
config.debug && event === 'Error' && console.log(action, name);
if (config.track){
const t = new Date()
name = name ? `&e_n=${name}` : '';
value = value ? `&e_v=${value}` : '';
fetch(`https://analytics.mattkirkland.co.uk/matomo.php?h=${t.getHours()}&m=${t.getMinutes()}&s=${t.getSeconds()}
&action_name=Settings%20Change&idsite=${ID_SITE}&rand=${Date.now()}&rec=1&uid=${config.UUID}&apiv=1
&e_c=${event}&e_a=${action}${name}${value}`)
.then(response => {
if (! response.ok) throw new Error('Network response was not ok', response);
})
.catch(error => console.log('Error posting tracking:', error))
}
}


async function createContextMenu(e) {
const target = e.target;
Expand Down Expand Up @@ -4860,3 +4825,5 @@ function track(event, action, name, value){
return false;
}

// Make config and displayLocationAddress available to the map script in index.html
export { config, displayLocationAddress, LOCATIONS };
Loading

0 comments on commit 89ece9c

Please sign in to comment.