Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add events once the app is loaded #398

Closed
AntouanK opened this issue Dec 2, 2020 · 4 comments
Closed

add events once the app is loaded #398

AntouanK opened this issue Dec 2, 2020 · 4 comments

Comments

@AntouanK
Copy link

AntouanK commented Dec 2, 2020

Hi there.

Thanks for your amazing work.

Quick question.
I have a SPA and I load the umami tracker. The problem is that even if I have the classes in the correct elements, umami doesn't see them because it looks for them before they exist.

More specifically the browser ( in time order ) :

Is there a way to run addEvents when my app is done?
As far as I can see that function is not exposed.
Am I missing something?

Thank you.

@mikecao
Copy link
Collaborator

mikecao commented Dec 3, 2020

The defer attribute should make the script load after the page. If that doesn't work, you can try dynamically writing the script tag after your app completely loads.

I'll look into exposes the event methods. I'm just concerned about potential duplicate stats. But I guess if you're calling it manually you know the risks.

@AntouanK
Copy link
Author

AntouanK commented Dec 3, 2020

yes, I ended up adding the script after my app has loaded.

I looked a bit into that tracker code.
You're right, I guess running addEvents multiple times will add duplicate listeners to elements potentially.

Could we maybe map them as we add them?
So in any subsequent call to addEvents we can use that map to clear the existing element listeners first?

Otherwise, I'm thinking that a proper solution might be for my app to add custom events, and not use the classes.
So that offloads the work to my app and not umami ( since the way to add custom events is already available )

btw, I'm using this for my HN reader --> https://hack.ernews.info/
code for the front-end is here https://gitlab.com/antouank/hni-ui ( the whole project is open source )
and the way I did it for now is this https://gitlab.com/antouank/hni-ui/-/blob/master/staticAssets/main.js#L108
but it's very hacky, so I want to have a proper solution.

Let me know what you think.

Thank you.

@mikecao
Copy link
Collaborator

mikecao commented Dec 4, 2020

Your solution is the right one. It ensures the script will always load last.

As for calling the methods,, using removeEvents right before addEvents should work.

@mikecao
Copy link
Collaborator

mikecao commented Dec 5, 2020

Updated in #405

@mikecao mikecao closed this as completed Dec 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants