Skip to content

Instantly share code, notes, and snippets.

@dekadentno
Last active February 1, 2019 05:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dekadentno/eb8e8019db57f216a62d0fc218969dc8 to your computer and use it in GitHub Desktop.
Save dekadentno/eb8e8019db57f216a62d0fc218969dc8 to your computer and use it in GitHub Desktop.
Google analytics vue and nuxt opt in (gdpr)
/*
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