about.js
4 weeks ago
admin-notifications.js
4 weeks ago
settings.js
4 weeks ago
smash-usage-session.js
4 weeks ago
support.js
4 weeks ago
smash-usage-session.js
55 lines
| 1 | /** |
| 2 | * Smash Usage Tracking: session duration and record-event from JS. |
| 3 | * |
| 4 | * @package TwitterFeed |
| 5 | */ |
| 6 | (function ($) { |
| 7 | 'use strict'; |
| 8 | |
| 9 | if (typeof window.ctfSmashUsageSession === 'undefined') { |
| 10 | return; |
| 11 | } |
| 12 | |
| 13 | var config = window.ctfSmashUsageSession; |
| 14 | var sessionStart = Date.now(); |
| 15 | var sessionSent = false; |
| 16 | |
| 17 | function sendSessionDuration() { |
| 18 | if (sessionSent) { |
| 19 | return; |
| 20 | } |
| 21 | sessionSent = true; |
| 22 | var durationSeconds = Math.round((Date.now() - sessionStart) / 1000); |
| 23 | if (durationSeconds < 3) { |
| 24 | return; |
| 25 | } |
| 26 | if (navigator.sendBeacon) { |
| 27 | var data = new FormData(); |
| 28 | data.append('action', 'ctf_smash_usage_record_session'); |
| 29 | data.append('nonce', config.nonce); |
| 30 | data.append('duration_seconds', durationSeconds); |
| 31 | navigator.sendBeacon(config.ajax_url, data); |
| 32 | } else { |
| 33 | $.post(config.ajax_url, { |
| 34 | action: 'ctf_smash_usage_record_session', |
| 35 | nonce: config.nonce, |
| 36 | duration_seconds: durationSeconds |
| 37 | }); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | function recordEvent(eventName) { |
| 42 | $.post(config.ajax_url, { |
| 43 | action: 'ctf_smash_usage_record_event', |
| 44 | nonce: config.event_nonce, |
| 45 | event_name: eventName |
| 46 | }); |
| 47 | } |
| 48 | |
| 49 | $(window).on('beforeunload pagehide', function () { |
| 50 | sendSessionDuration(); |
| 51 | }); |
| 52 | |
| 53 | window.ctfSmashUsageRecordEvent = recordEvent; |
| 54 | })(jQuery); |
| 55 |