Visitar URL original
How do I limit collection for authenticated pages in an SPA? · Issue #168 · digital-analytics-program/gov-wide-code · GitHub
Skip to content

How do I limit collection for authenticated pages in an SPA? #168

Open

Description

@mdmower-csnw

The getting started guide says...

Beyond sign-in pages, implementation of the DAP code on authenticated pages is permitted on a case-by-case basis only. Email the DAP team at dap@gsa.gov to request their approval and coordinate the prerequisite tests.

For an SPA (single page application), where the script loads once, is there any way for me to make DAP aware of paths where collection should not occur?

Activity

  1. mdmower-csnw commented on May 13, 2025

    @mdmower-csnw
    Author

    I still hope to get a response from a maintainer, but for any other users that stumble upon this question...

    I'm able to work around this issue by setting window[`ga-disable-${gtag_id}`] = true; prior to navigating to authenticated-only pages and window[`ga-disable-${gtag_id}`] = false; when navigating to anonymously accessible pages. The GTag ID can be one of two values, depending on whether dap-dev-env is set, so a small lookup helps:

    function findGtagId() {
      for (const script of document.scripts) {
        // https://github.com/digital-analytics-program/gov-wide-code/blob/1817f8a53949c98953ec6b6015135c686805c99d/Universal-Federated-Analytics.js#L91
        if (script.src.startsWith('https://www.googletagmanager.com/gtag/js?id=')) {
          const id = new URL(script.src).searchParams.get('id');
          if (id) return id;
        }
      }
    }

    And specifically for React, if your private routes are wrapped by a component, a single useEffect hook can both disable and reenable analytics when the component is mounted and unmounted, respectively:

      useEffect(() => {
        const gtagId = findGtagId();
        if (!gtagId) return;
    
        window[`ga-disable-${gtagId}`] = true;
        return () => {
          window[`ga-disable-${gtagId}`] = false;
        };
      }, []);

    The analytics documentation makes it sound like this approach wouldn't work since it is set after GTag has loaded, but in practice, it does seem to work fine for SPAs.

    Aside: the approach that I thought would work...

    function gtag(){dataLayer.push(arguments);}
    // does not work
    gtag('set', { 'send_page_view': false });

    ... does not work. In this case, I believe it is because history change events cause the page view events to be sent, and that process doesn't appear to check this setting prior to collection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    • Status
      Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions