Created
February 22, 2018 16:27
-
-
Save RomkeVdMeulen/1c43ca25858059ceed3723ca9e699eab to your computer and use it in GitHub Desktop.
Aurelia Custom Attribute for registering events with Google Universal Analytics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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