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 mamoto tracking to frontend #1040

Merged
merged 5 commits into from
Dec 11, 2023
Merged

Conversation

spwoodcock
Copy link
Member

@spwoodcock spwoodcock commented Dec 11, 2023

Fixes #942

For website analytics.

This only activates on the prod setup (fmtm.hotosm.org).
The approach I settled on:

const MatomoTrackingInit = () => {
  useEffect(() => {
    if (import.meta.env.MODE !== 'development' && import.meta.env.BASE_URL === 'fmtm.hotosm.org') {
      return;
    }
    // Set matomo tracking id
    window.site_id = environment.mamotoTrackingId;// Create optout-form div for banner
    const optoutDiv = document.createElement('div');
    optoutDiv.id = 'optout-form'; // Set an ID if needed
    document.body.appendChild(optoutDiv);// Load CDN script after
    const script = document.createElement('script');
    script.src = 'https://cdn.hotosm.org/tracking-v3.js';
    document.body.appendChild(script);
    // Manually trigger DOMContentLoaded, that script hooks
    // https://github.com/hotosm/matomo-tracking/blob/9b95230cb5f0bf2a902f00379152f3af9204c641/tracking-v3.js#L125
    script.onload = () => {
      optoutDiv.dispatchEvent(
        new Event('DOMContentLoaded', {
          bubbles: true,
          cancelable: true,
        }),
      );
    };// Cleanup on unmount
    return () => {
      document.body.removeChild(optoutDiv);
      document.body.removeChild(script);
    };
  }, []);return null; // Renders nothing
};// The main App render
ReactDOM.render(
  <Provider store={store}>
    <PersistGate loading={null} persistor={persistor}>
      <RouterProvider router={routes} />
      <MatomoTrackingInit />
      <SentryInit />
    </PersistGate>
  </Provider>,
  document.getElementById('app'),
);

@spwoodcock spwoodcock self-assigned this Dec 11, 2023
@github-actions github-actions bot added the frontend Related to frontend code label Dec 11, 2023
@spwoodcock spwoodcock merged commit 6c8de54 into development Dec 11, 2023
2 of 3 checks passed
@spwoodcock spwoodcock deleted the feat/mamoto-tracking branch December 11, 2023 20:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
frontend Related to frontend code
Projects
Development

Successfully merging this pull request may close these issues.

Add matomo tracking code
1 participant