miguelzakharia / aurelia-google-analytics

An Aurelia.io plugin that adds Google Analytics page tracking to your project.
MIT License
29 stars 23 forks source link

Set user Id and custom dimension after plugin instantiation #22

Open KevinStong opened 7 years ago

KevinStong commented 7 years ago

Hi Everyone,

Was curious on what the community insight/feedback would be around setting data with the plugin after instantiation in main or after lazy initialization of the plugin in the page lifecycle of a module?

I was looking to pass a custom dimension for the user's role on our site in addition to the userId variable to track users across devices. The problem is that I dont get this data until the user is logged in which occurs in app.js after the events in main.js which has the Framework configuration for loading the plugin. Now, I could easily do the following:

ga('set', 'userId', '11111')

in the activate of the app.js module but I was looking for a way to keep the google analytics concerns internal to the plugin.

Another thought on this was to lazy load the plugin in the app.js activation using the FrameworkConfiguration class from aurelia-framwork:

activate() {
  return new FrameworkConfiguration(this.aurelia).plugin('aurelia-google-analytics').apply();
}

then pass in the variables via init() as these params can also be included on the ga('create') initialization call but I was concerned about having this plugin instantiated outside of main.js where I am doing the remainder of the work setting up other plugins. I also didn't really like this sprinkled about in the application requiring someone to have to go looking for it.

thoughts on this? Curious on how others are handing this as well...

thanks!

Kevin

miguelzakharia commented 7 years ago

Hi Kevin,

I came across the exact scenario you bring up and went through the same thought process as you. I ended up just using ga('set', 'userId', '11111'). I couldn't find a better way to do it at the time. I also considered creating a function on the plugin that can be called to set the user after initialization. It would basically just be a wrapper around the ga('set'...). It would hide the internals, but is not providing much benefit other than encapsulation. I felt that it might lead to a slippery slope of wrapping all of the features of GA in this manner which I think is not very elegant.

I'm certainly open to suggestions and collaborating on a better abstraction.

LukevanTricht commented 6 years ago

This would be really nice, especially for those of us using Typescript, as we can't call the ga object because it's not declared anywhere, and thus the Typescript compiler complains and refuses. So either a method which exposes ga, or methods which give us more access to the ga object would be fantastic.

dantheother commented 4 years ago

@LukevanTricht does https://www.npmjs.com/package/@types/google.analytics help?

LukevanTricht commented 4 years ago

@LukevanTricht does https://www.npmjs.com/package/@types/google.analytics help?

I can imagine that would do exactly what I wanted, but unfortunately I no longer work for the company I was building that software for, so I have no way of testing at this time.