Emitting a custom event for anchor tracking #3538
-
Contribution guidelines
I want to suggest an idea and checked that ...
DescriptionI'm enabling Anchor Tracking as part of the migration to MKdocs Material 8.x. This feature can help give a more accurate analytics on which sections are more popular than other - however, I've just noticed it's rewriting the history state. With my limited research on history rewriting, I can't hook up to any event (e.g. Would you consider creating a custom event so we can use this great feature for analytics purpose too? Thank you as always for this great theme!! Use CasesUse the new anchor tracking feature to send more accurate page view for sections being scrolled to. Screenshots / MockupsNo response |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 7 replies
-
Moving to discussion, since this is a customization request. It's definitely possible to achieve, as the whole architecture is based on observables. In essence, you can observe the table of contents element and check which is the active section. |
Beta Was this translation helpful? Give feedback.
-
That helps already ;-) I know where to start now - thank you!
…On Fri, 4 Feb 2022 at 17:54, Martin Donath ***@***.***> wrote:
#3429 (comment)
<#3429 (comment)>
provides some notes on the general extensibility mechanism and an example
to detect color palette switches, which is similar. I'm afraid I can't hack
something together for your purpose as I'm really short on time right now,
but my comment should yield some insight on how the theme exposes component
events. component$ is the observable you're looking for, and within that
you need to filter for the table of contents component. Maybe someone else
can cobble something up.
—
Reply to this email directly, view it on GitHub
<#3538 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAZPQBFS2O4BIB26RR3FCDDUZQAGBANCNFSM5NSH4HIQ>
.
You are receiving this because you authored the thread.Message ID:
***@***.***
com>
|
Beta Was this translation helpful? Give feedback.
-
Sure thing! I need to learn RxJs first and its operators, then revisit
source code to see what’s being exported besides “location$, viewport$”.
I noticed things like takeUntil and denounce from RxJs aren’t exported - so
just need some trial and error until I figure it out ;-)
…On Fri, 4 Feb 2022 at 18:20, Martin Donath ***@***.***> wrote:
Awesome 😊 Feel free to share it when you figure it out!
—
Reply to this email directly, view it on GitHub
<#3538 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAZPQBCFKTHGQHE76YLIQ2LUZQDFXANCNFSM5NSH4HIQ>
.
You are receiving this because you authored the thread.Message ID:
***@***.***
com>
|
Beta Was this translation helpful? Give feedback.
-
Nah I’d say don’t - it’s not the core premise, and as you pointed out it’ll
highly likely become RxJs support from the little I just read from their
docs.
I find events easier to decouple on that front, it’s agnostic. Maybe
documenting and emitting a set of events people can listen to with plain
JS, so they can extend as they please (search, anchor tracking, etc) — that
can open up a more flexible customisation without burdening you
I’ll look up the codebase to learn more about the anchor tracking came
together and use as a basis.
…On Fri, 4 Feb 2022 at 19:04, Martin Donath ***@***.***> wrote:
Yeah, all used operators are bundled. I thought about exporting them, but
then I figured that 99.9% of users won't need them anyway, as RxJS is a
pretty tough end boss to beat, so I discontinued that idea. Maybe I revisit
it in the future, but I'm not sure I want to burden myself with an
increased need for support for users trying to make their RxJS pipelines
work. It's a quite bumpy ride 😉
—
Reply to this email directly, view it on GitHub
<#3538 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAZPQBARJLRPRURGFN6YQEDUZQIJ7ANCNFSM5NSH4HIQ>
.
You are receiving this because you authored the thread.Message ID:
***@***.***
com>
|
Beta Was this translation helpful? Give feedback.
-
location$.subscribe is under custom analytics section, which I clearly missed because I used that on 6.x only (changed on 7.x for RxJs from Webpack). https://squidfunk.github.io/mkdocs-material/setup/setting-up-site-analytics/ My initial attempt at using location$ didn't work because I wasn't using instant loading which all makes sense now. |
Beta Was this translation helpful? Give feedback.
-
Found a solution using monkeypatching (not great, not terrible; gets the job done). var replaceState = history.replaceState;
history.replaceState = function () {
const url = location.href
var section = url.split("#")[1]
if (section != undefined) {
console.log(`TOC section changed: ${section}, url: ${url}`)
}
replaceState.apply(history, arguments);
}; Lessons learned from this experiment in case it's helpful to anyone else:
If you think this could be useful to someone under Custom Analytics or any FAQ section, I'm more than happy to make a PR to document this. Thanks for the pointers @squidfunk ! |
Beta Was this translation helpful? Give feedback.
Found a solution using monkeypatching (not great, not terrible; gets the job done).
Lessons learned from this experiment in case it's helpful to anyone else:
history.replaceState
to seamlessly rewrite current history so the browser doesn't have to do additional checks whether the URL exists; downsid…