| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
const { recordVirtualViewBeaconURI } = require( '../config.json' ); |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
class ViewLogger { |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
constructor( windowObject ) { |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
this.wasLastViewLogged = false; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
this.viewStartTime = 0; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
this.viewDuration = 0; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
this.url = ''; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
this.window = windowObject; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
unview() { |
| 68 |
if ( !this.wasLastViewLogged ) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
this.wasLastViewLogged = false; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
startViewDuration() { |
| 79 |
this.viewStartTime = Date.now(); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
stopViewDuration() { |
| 86 |
if ( this.viewStartTime ) { |
| 87 |
this.viewDuration += Date.now() - this.viewStartTime; |
| 88 |
this.viewStartTime = 0; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
recordViewDuration() { |
| 96 |
let url; |
| 97 |
|
| 98 |
this.stopViewDuration(); |
| 99 |
|
| 100 |
if ( recordVirtualViewBeaconURI ) { |
| 101 |
try { |
| 102 |
url = new URL( recordVirtualViewBeaconURI, location ); |
| 103 |
url.searchParams.set( 'duration', this.viewDuration ); |
| 104 |
url.searchParams.set( 'uri', this.url ); |
| 105 |
} catch ( e ) { |
| 106 |
|
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
try { |
| 111 |
navigator.sendBeacon( url.toString() ); |
| 112 |
} catch ( e ) { |
| 113 |
$.ajax( { |
| 114 |
type: 'HEAD', |
| 115 |
url: url.toString() |
| 116 |
} ); |
| 117 |
} |
| 118 |
|
| 119 |
mw.log( 'Image has been viewed for ', this.viewDuration ); |
| 120 |
} |
| 121 |
|
| 122 |
this.viewDuration = 0; |
| 123 |
|
| 124 |
this.unview(); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
attach( url ) { |
| 133 |
this.url = url; |
| 134 |
this.startViewDuration(); |
| 135 |
|
| 136 |
$( this.window ) |
| 137 |
.off( '.mmv-view-logger' ) |
| 138 |
.on( 'beforeunload.mmv-view-logger', () => this.recordViewDuration() ) |
| 139 |
.on( 'focus.mmv-view-logger', () => this.startViewDuration() ) |
| 140 |
.on( 'blur.mmv-view-logger', () => this.stopViewDuration() ); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
unattach() { |
| 147 |
$( this.window ).off( '.mmv-view-logger' ); |
| 148 |
this.stopViewDuration(); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
setLastViewLogged( wasEventLogged ) { |
| 157 |
this.wasLastViewLogged = wasEventLogged; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
module.exports = ViewLogger; |