Skip to content

Instantly share code, notes, and snippets.

@antlis
Last active November 26, 2019 18:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antlis/56cd3963182ebaeadec55a23efbb60f9 to your computer and use it in GitHub Desktop.
Save antlis/56cd3963182ebaeadec55a23efbb60f9 to your computer and use it in GitHub Desktop.
Google analytics pug mixin
mixin ga(id)
//- Pass your ga id, as an attribute, eg. +ga('yourid12345')
script.
(function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]=
function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date;
e=o.createElement(i);r=o.getElementsByTagName(i)[0];
e.src='https://www.google-analytics.com/analytics.js';
r.parentNode.insertBefore(e,r)}(window,document,'script','ga'));
ga('create', !{id});ga('send','pageview');
@aetherwu
Copy link

aetherwu commented Aug 9, 2019

For any others that are newbie like me, here is a way to apply it in .pug file:

include your_path/ga.pug
+ga

The first one to import it and the second line to active it.

@antlis
Copy link
Author

antlis commented Aug 15, 2019

For any others that are newbie like me, here is a way to apply it in .pug file:

include your_path/ga.pug
+ga

The first one to import it and the second line to active it.

Hi, I updated it, now you can pass your id as an attribute to mixin, +ga('yourid12345')

@antlis
Copy link
Author

antlis commented Aug 15, 2019

Also here is pug google analytics mixin, that will output script like you can find in HTML5 boilerplate.

@aetherwu
Copy link

Also here is pug google analytics mixin, that will output script like you can find in HTML5 boilerplate.

That makes the code more flexible, thanks a lot! :D

@antlis
Copy link
Author

antlis commented Aug 28, 2019

Also here is pug google analytics mixin, that will output script like you can find in HTML5 boilerplate.

That makes the code more flexible, thanks a lot! :D

No problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment