Last active
February 1, 2019 05:57
-
-
Save dekadentno/eb8e8019db57f216a62d0fc218969dc8 to your computer and use it in GitHub Desktop.
Google analytics vue and nuxt opt in (gdpr)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Had some problems with implementing google analytics opt-in, as in nuxt (nor vue) doesn't support that out of the box, | |
so I found a hacky method that works. | |
Idea: | |
1. user comes to the page, the famous "this page is using cookies" widget is shown | |
2. when the user clicks "accept cookies", the google analytics script should be injected into the code | |
Solution: | |
1. put your google analytics js script in the static folder of your project | |
2. execute the following code after the user clicks "accept cookies" | |
*/ | |
let s = document.createElement("script") | |
s.type = "text/javascript" | |
s.src = "/googleanalytics.js" | |
let first = window.document.getElementsByTagName('head')[0].firstChild | |
window.document.getElementsByTagName('head')[0].insertBefore(s, first) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment