How to provide custom names for page view events in Azure App Insights?

前端 未结 3 648
说谎
说谎 2021-01-19 14:31

By default App Insights use page title as event name. Having dynamic page names, like \"Order 32424\", creates insane amount of event types.

Documentation on the mat

3条回答
  •  独厮守ぢ
    2021-01-19 15:21

    Here's one work-around, if you're using templates to render your /orders/12345 pages:

    appInsights.trackPageView({name: TEMPLATE_NAME });
    

    Another option, perhaps better suited for a SPA with react-router:

    const Tracker = () => {
      let {pathname} = useLocation();
      pathname = pathname.replace(/([/]orders[/])([^/]+), "$1*");  // handle /orders/NN/whatever
      pathname = pathname.replace(/([/]foo[/]bar[/])([^/]+)(.*)/, "$1*");  // handle /foo/bar/NN/whatever
      useEffect(() => {
        appInsights.trackPageView({uri: pathname});
      }, [pathname]);
      return null;
    }
    

提交回复
热议问题