angulartics / angulartics2

Vendor-agnostic analytics for Angular2 applications.
MIT License
1.01k stars 193 forks source link

data interceptor support #84

Open ritz078 opened 7 years ago

ritz078 commented 7 years ago

I want to send some common data in all the events like id or filterParams. Anyway I can include that in all the events without writing those everywhere ?

JonnyBGod commented 7 years ago

Should not be difficult to provide some king of injection point to the ReplaySubject. The idea sounds nice, but I am concerns that this feature would probably only work with a few providers.

We need to evaluate it better.

I will take a closer look when I get the time, meanwhile it would be helpful to have some descriptions of use cases.

What are your thought on this? @NathanWalker @rolandoldengarm @jylinman

ritz078 commented 7 years ago

I would want this for Google Analytics. Most of the time in a large app where some data needs to be sent in whole app or part of the app like 'cookies', 'filters', 'userId', 'timestamp' etc.

panbanda commented 7 years ago

I had been thinking about a defaults / commons object that can be passed to each provider or ignored altogether. I have been slowly working through this for things like Keen.io, for example, who have a very fluid data model that would need to be formatted before being sent. I don't know if it would be good to have some sort of serializer / formatter callback that can be specific to user instead of provider. Thoughts? I could be off in terms of what you're looking to do...

ritz078 commented 7 years ago

I will be speaking here about google analytics.

In every label I want to send an object which has few keys those are common to all. I would like to set those keys somewhere that are merged with the label object before sending it to GA.

JonnyBGod commented 7 years ago

I think we can change Angulartics2 service a bit and plug in a map to each Replay subject to public functions on the service that can be overwritten by the user to process the data or trigger any enter functions.

Trick with this is that the user can then easily break the data.

But I believe this would solve both your use cases.

JonnyBGod commented 7 years ago

quick example:

changes to Angulartics2:

...
public pageTrackMap(data: any): any {
  return data;
};
public pageTrack: ReplaySubject<any> = new ReplaySubject(10).map(pageTrackMap);
...

in you component or service you could than do:

constructor(private angulartics2: Angulartics2) {
  angulartics2.pageTrackMap = function(data: any): any {
    // do whatever with data
   return data;
  }
}
JonnyBGod commented 7 years ago

There might be better solutions to this though. I need to study RxJs a bit more.