There’s a little bit about this here.
Javascripts targeting the footer doesn't work after page transitions - #4 by Johani
When you use this
api.onAppEvent("inserted-custom-html", () => {
// some code
});
You have to specify which custom HTML you want to target - different events fire based on that.
In this case, you want to target the footer. Can you try this and see if it works for you?
api.onAppEvent("inserted-custom-html:footer", () => {
// some code
});