Skip to content

Instantly share code, notes, and snippets.

@RomkeVdMeulen
Created February 22, 2018 16:27
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 RomkeVdMeulen/1c43ca25858059ceed3723ca9e699eab to your computer and use it in GitHub Desktop.
Save RomkeVdMeulen/1c43ca25858059ceed3723ca9e699eab to your computer and use it in GitHub Desktop.
Aurelia Custom Attribute for registering events with Google Universal Analytics
import {autoinject, bindable} from "aurelia-framework";
declare const ga: (
command: "send",
type: "event",
category?: string,
action?: string,
label?: string,
) => void;
@autoinject
export class AnalyticsCustomAttribute {
@bindable
on = "click";
@bindable({primaryProperty: true})
event?: string;
@bindable
category?: string;
@bindable
label?: string;
private listener: (event: Event) => void;
constructor(private element: Element) {
this.listener = this.onEvent.bind(this);
}
bind() {
this.element.addEventListener(this.on, this.listener);
}
unbind() {
this.element.removeEventListener(this.on, this.listener);
}
onEvent() {
ga("send", "event", this.category, this.event, this.label);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment