Support matomo media analytics in embed too
This commit is contained in:
		| @ -1,53 +1,32 @@ | ||||
| import { initMatomo } from './utils' | ||||
|  | ||||
| function register ({ registerHook, peertubeHelpers }) { | ||||
|   initMatomo(registerHook, peertubeHelpers) | ||||
|     .catch(err => console.error('Cannot initialize Matomo', err)) | ||||
|   init(registerHook, peertubeHelpers) | ||||
|     .catch(err => console.error('Cannot initialize Matomo plugin', err)) | ||||
| } | ||||
|  | ||||
| export { | ||||
|   register | ||||
| } | ||||
|  | ||||
| function initMatomo (registerHook, peertubeHelpers) { | ||||
|   return peertubeHelpers.getSettings() | ||||
|     .then(s => { | ||||
|       if (!s || !s['site-id'] || !s['url']) { | ||||
|         console.error('Matomo settings are not set.') | ||||
|         return | ||||
|       } | ||||
|  | ||||
|       const matomoUrl = s['url'] | ||||
|       const siteId = s['site-id'] | ||||
|  | ||||
|       window._paq = window._paq || []; | ||||
|       window._paq.push(['trackPageView']); | ||||
|       window._paq.push(['enableLinkTracking']); | ||||
|       (function() { | ||||
|         var u = matomoUrl + '/'; | ||||
|         window._paq.push(['setTrackerUrl', u+'matomo.php']); | ||||
|         window._paq.push(['setSiteId', siteId]); | ||||
|         var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; | ||||
|         g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); | ||||
|       })(); | ||||
| async function init (registerHook, peertubeHelpers) { | ||||
|   const success = await initMatomo(peertubeHelpers) | ||||
|   if (!success) return | ||||
|  | ||||
|   registerHook({ | ||||
|     target: 'action:router.navigation-end', | ||||
|     handler: function (params) { | ||||
|       window._paq.push(['setDocumentTitle', window.document.title]); | ||||
|       window._paq.push(['setCustomUrl', '/' + window.location.hash.substr(1)]); | ||||
|       window._paq.push(['setCustomUrl', params.path]); | ||||
|       window._paq.push(['trackPageView']); | ||||
|     } | ||||
|   }) | ||||
|  | ||||
|       registerHook({ | ||||
|         target: 'action:router.navigation-end', | ||||
|         handler: function (params) { | ||||
|           window._paq.push(['setDocumentTitle', window.document.title]); | ||||
|           window._paq.push(['setCustomUrl', params.path]); | ||||
|           window._paq.push(['trackPageView']); | ||||
|         } | ||||
|       }) | ||||
|  | ||||
|       registerHook({ | ||||
|         target: 'action:video-watch.player.loaded', | ||||
|         handler: function () { | ||||
|           window._paq.push(['MediaAnalytics::scanForMedia', window.document]); | ||||
|         } | ||||
|       }) | ||||
|     }) | ||||
|  | ||||
|   registerHook({ | ||||
|     target: 'action:video-watch.player.loaded', | ||||
|     handler: function () { | ||||
|       window._paq.push(['MediaAnalytics::scanForMedia', window.document]); | ||||
|     } | ||||
|   }) | ||||
| } | ||||
|  | ||||
|  | ||||
							
								
								
									
										22
									
								
								peertube-plugin-matomo/client/embed-client-plugin.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								peertube-plugin-matomo/client/embed-client-plugin.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,22 @@ | ||||
| import { initMatomo } from './utils' | ||||
|  | ||||
| function register ({ registerHook, peertubeHelpers }) { | ||||
|   init(registerHook, peertubeHelpers) | ||||
|     .catch(err => console.error('Cannot initialize Matomo plugin', err)) | ||||
| } | ||||
|  | ||||
| export { | ||||
|   register | ||||
| } | ||||
|  | ||||
| async function init (registerHook, peertubeHelpers) { | ||||
|   const success = await initMatomo(peertubeHelpers) | ||||
|   if (!success) return | ||||
|  | ||||
|   registerHook({ | ||||
|     target: 'action:embed.player.loaded', | ||||
|     handler: function () { | ||||
|       window._paq.push(['MediaAnalytics::scanForMedia', window.document]); | ||||
|     } | ||||
|   }) | ||||
| } | ||||
							
								
								
									
										33
									
								
								peertube-plugin-matomo/client/utils.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								peertube-plugin-matomo/client/utils.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,33 @@ | ||||
| function initMatomo(peertubeHelpers) { | ||||
|   return peertubeHelpers.getSettings() | ||||
|     .then(s => { | ||||
|       if (!s || !s['site-id'] || !s['url']) { | ||||
|         console.error('Matomo settings are not set.') | ||||
|         return false | ||||
|       } | ||||
|  | ||||
|       const matomoUrl = s['url'] | ||||
|       const siteId = s['site-id'] | ||||
|  | ||||
|       window._paq = window._paq || [] | ||||
|       window._paq.push(['trackPageView']) | ||||
|       window._paq.push(['enableLinkTracking']); | ||||
|       (function () { | ||||
|         var u = matomoUrl + '/' | ||||
|         window._paq.push(['setTrackerUrl', u + 'matomo.php']) | ||||
|         window._paq.push(['setSiteId', siteId]) | ||||
|         var d = document, g = d.createElement('script'), s = d.getElementsByTagName('script')[0] | ||||
|         g.type = 'text/javascript'; g.async = true; g.defer = true; g.src = u + 'matomo.js'; s.parentNode.insertBefore(g, s) | ||||
|       })() | ||||
|  | ||||
|       window._paq.push(['setDocumentTitle', window.document.title]) | ||||
|       window._paq.push(['setCustomUrl', '/' + window.location.hash.substr(1)]) | ||||
|       window._paq.push(['trackPageView']) | ||||
|  | ||||
|       return true | ||||
|     }) | ||||
| } | ||||
|  | ||||
| export { | ||||
|   initMatomo | ||||
| } | ||||
		Reference in New Issue
	
	Block a user