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