I'm a developer and I often write JSFiddles for example. I use Google Analytics on my personal website to monitor traffic. jsfiddle.net is not my own domain, but in a way, snippets I'm publishing on belongs to me.

How can I make traffic my own JSFiddles' generate counts for my own domain's one? In other words, when someone visits a JSFiddle I have written, I'd like it counts for one visits on Google Analytics.

3 Answers 3


It is simple. In your JSFiddle you put your code. I mean Google Analytics of your website. something like this .change the id to yours.

       (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

      ga('create', 'UA-your id-1', 'http://abernier.name/');
      ga('send', 'pageview');
  • from what I can read here: developers.google.com/analytics/devguides/collection/… it requires I use the Analytics.js version of google Analytics, right?
    – abernier
    Commented Feb 2, 2014 at 17:35
  • Polluting the code with garbage is so wrong that I think I'm reading your answer wrong. Are you actually suggesting to put google analytics code on a jsfiddle code?
    – PatomaS
    Commented Feb 5, 2014 at 1:09
  • @PatomaS, do you have a better option?
    – abernier
    Commented Feb 5, 2014 at 1:18

Since you are publishing examples, the best way to use them in your site is to have clear descriptions of what the code does, elaborate the idea that you are showing, the reason why you choose something instead of another option, some caveats, what you accomplish with that code, how can you call it, or integrate it, or whatever is related to the piece of code that you are publishing.

There are tons of code examples on Internet, but very little good explanations. Help to change that. If you do, that will help you, and your readers much more.

Lets consider some scenarios.

  1. People get to your site, read the article/post and go to your jsfiddle/code. From the traffic perspective, you already have what you want, you already had a visitor. If your explanation and your code are good enough, that person will come back some other time. If your explanation is bad, most probably won't even go to see your code. I wouldn't. Adding extra code to your original code, will make it more complex, and require that you explain that some parts are not required, which implies more work from your part and the reader. There is almost never a good reason to do extra work.

  2. People get to your jsfiddle/code, without going to your site. What is that code for? how did they get there? why there is an analytics code on the script? do I need that part for the code to work? if not, why did the programmer put it there?. Plus the above consideration, you had to add more explanations to the code to make clear which part is needed and which is not.

To the second scenario, I have to add, how many times have you made a search and there have been jsfiddle results on it? or pastebin, or sqlfiddle, or any other site similar?. In my experience, less than 1%, only when the search is extremely specific and there are like 4 results on the whole Internet (crawled). And they are never useful. So, is it worth polluting your code for, how many visits?.

Now, for any scenario, assume that you add a brief comment, adding your site, your email or the url of the article where the code is explained, that is much more useful because the visitor can keep it and have a reminder of your site/name/article and visit it from time to time. Even more, if your code is complex enough and can be improved, you may add in the comment something like there will be new versions referenced on the original article, which will, in turn, grant more visits.

There are more options, but this is getting too long already. The important part is that if you just try to get traffic/visitors/views without providing something useful for the user, that is an empty experience and doesn't benefit anybody.

  • Hey @PatomaS, I guess you are not answering the initial question, but give me a personal advice about my website? :)
    – abernier
    Commented Feb 5, 2014 at 1:16
  • well, yes and no, the thing is, if you have a good article/post, that is going to draw traffic to the jsfiddle code, but, this is the important part, I'm assuming that since you say that you use jsfiddle a lot, you will have an account, and so, people can see your username there and follow that information to your site. You can even put your site in a comment on the code, much better that polluting it with the other code. Also, how many times have you seen pages/codes from jsfiddle and similar places coming up as result of searches?
    – PatomaS
    Commented Feb 5, 2014 at 1:24
  • i'll update my answer since I have more space there.
    – PatomaS
    Commented Feb 5, 2014 at 1:25
  • I understand now :) I think that's a good and wise advice to do so, and a kind of workaround. But, you know, sometimes we're just lazy and even if I follow your advice for some of my jsfiddles, I'm not sure doing it for all of them... But thx for that answer, that's smart ;)
    – abernier
    Commented Feb 5, 2014 at 1:34

JSFiddle does not offer Google Analytics tracking as a feature, but it can be done.

Create a .js file with your Google Analytics tracking code and save this to a publicly accessible web hosting location.

Then add your .js file as an external resource to all of your JSFiddles. Sadly this does need to be done to each Fiddle, BUT it keeps your code out of the code, and if you ever need to make changes to your GA code it's one change for all Fiddles.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.