Keep your Html clean even for Google Analytics 😒

It's out there, many web articles like this and even that actually recommend it. 🤷

But what ever happened to separating view from logic? 😐

It seems when it comes to google analytics, all logic jumps out the wind.🤔

I'm talking about tracking events using inline JavaScript via onclick attributes.

why do we do this to ourselves?

Why oh why do we do this to ourselves?

I'm still trying to find an advantage to doing it this way 🤔. I mean, is there something special that occurs when you use the onclick attribute that can't be achieved any other way?

behold, an alternative

Pending when I find the reason, behold ... an alternative.

It's so simple and has lots of advantages, without clogging up your HTML with inline JavaScript.

I hereby present ga-click, which was originally an Angular Directive (now in pure JS), restricted to Html Element Attributes that executes google analytic commands beautifully.

You can view a demo here. Forgive my UI 😄 I know it needs work 😋

Now you can write semantic HTML while still tracking your user interactions via google search.

You also have the GA Commands in a central JavaScript file from which you can manage them all :) instead of having them scattered over multiple files in your project.

Have fun coding!

Show Comments