Skip to content

Commit 87b90a7

Browse files
Merge pull request AmazingDreams#111 from AdamSGit/feature/crossorigin
[feature/crossorigin] Add crossorigin attribute support
2 parents b5bf3e1 + 29a8c9a commit 87b90a7

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,11 @@ Vue.use(VueMatomo, {
123123
// return null
124124
// }
125125
// }
126-
trackSiteSearch: false
126+
trackSiteSearch: false,
127+
128+
// Set this to include crossorigin attribute on the matomo script import
129+
// Default: undefined, possible values : 'anonymous', 'use-credentials'
130+
crossOrigin: undefined,
127131
});
128132

129133
// Now you can access piwik api in components through

src/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ const defaultOptions = {
1616
userId: undefined,
1717
cookieDomain: undefined,
1818
domains: undefined,
19-
preInitActions: []
19+
preInitActions: [],
20+
crossOrigin: undefined
2021
}
2122

2223
export const matomoKey = 'Matomo'
@@ -180,7 +181,7 @@ export default function install (Vue, setupOptions = {}) {
180181

181182
options.preInitActions.forEach((action) => window._paq.push(action))
182183

183-
loadScript(trackerScript)
184+
loadScript(trackerScript, options.crossOrigin)
184185
.then(() => piwikExists())
185186
.then(() => initMatomo(Vue, options))
186187
.catch((error) => {

src/utils.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ export function getMatomo () {
22
return window.Piwik.getAsyncTracker()
33
}
44

5-
export function loadScript (trackerScript) {
5+
export function loadScript (trackerScript, crossOrigin = undefined) {
66
return new Promise((resolve, reject) => {
77
const script = document.createElement('script')
88
script.async = true
99
script.defer = true
1010
script.src = trackerScript
1111

12+
if (crossOrigin && ['anonymous', 'use-credentials'].includes(crossOrigin)) {
13+
script.crossOrigin = crossOrigin
14+
}
15+
1216
const head = document.head || document.getElementsByTagName('head')[0]
1317
head.appendChild(script)
1418

0 commit comments

Comments
 (0)