All files monitoring.js

98.07% Statements 51/52
95.91% Branches 47/49
100% Functions 10/10
97.91% Lines 47/48

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218                37x 37x             21x   21x 2x 2x     19x 19x                                                                       9x 8x     8x     3x       5x   2x         4x 1x       4x 1x     4x                                                                                         19x 19x     1x             24x 24x 21x           3x       3x         25x   25x 20x 5x 1x 4x 3x   1x           21x 21x 1x       20x         3x   2x             4x   3x               2x   1x         2x   1x      
/* MONITORING.JS - Error Monitoring with Sentry =========================================
   Copyright 2025, Mark Forscher */
/* ======================================================================================= */
 
import * as Sentry from '@sentry/browser';
 
export class Monitoring {
  constructor() {
    this.enabled = false;
    this.dsn = null;
  }
 
  // Initialize Sentry error monitoring
  init() {
    // Check for Sentry DSN in environment or meta tag
    // For static sites, DSN can be set via meta tag or build-time replacement
    this.dsn = this.getDSN();
 
    if (!this.dsn) {
      console.log('Sentry monitoring disabled (no DSN configured)');
      return;
    }
 
    try {
      Sentry.init({
        dsn: this.dsn,
 
        // Set environment based on hostname
        environment: this.getEnvironment(),
 
        // Release version (can be set during build)
        release: this.getRelease(),
 
        // Performance monitoring sample rate (10% of transactions)
        tracesSampleRate: 0.1,
 
        // Error sample rate (100% - capture all errors)
        sampleRate: 1.0,
 
        // Integrations
        integrations: [
          // Breadcrumbs for debugging context
          Sentry.breadcrumbsIntegration({
            console: false, // Don't capture console logs as breadcrumbs
            dom: true,      // Capture DOM events
            fetch: true,    // Capture fetch requests
            history: true,  // Capture navigation
            xhr: true       // Capture XHR requests
          }),
 
          // Capture global errors
          Sentry.globalHandlersIntegration({
            onerror: true,
            onunhandledrejection: true
          })
        ],
 
        // Before sending events, filter out noisy errors
        beforeSend(event, hint) {
          // Filter out browser extension errors
          if (event.exception && event.exception.values) {
            const message = event.exception.values[0]?.value || '';
 
            // Ignore common third-party script errors
            if (message.includes('chrome-extension://') ||
                message.includes('moz-extension://') ||
                message.includes('safari-extension://')) {
              return null;
            }
 
            // Ignore Ad Blocker errors
            if (message.includes('adsbygoogle') ||
                message.includes('googleads')) {
              return null;
            }
          }
 
          // Anonymize IP address for privacy compliance
          if (event.user) {
            delete event.user.ip_address;
          }
 
          // Strip IP from request context
          if (event.request) {
            delete event.request.env?.REMOTE_ADDR;
          }
 
          return event;
        },
 
        // Ignore specific error URLs
        ignoreErrors: [
          // Browser extensions
          'top.GLOBALS',
          'chrome-extension',
          'moz-extension',
 
          // Facebook
          'fb_xd_fragment',
 
          // Random plugins/extensions
          'instantSearchSDKJSBridgeClearHighlight',
 
          // See: http://blog.errorception.com/2012/03/tale-of-unfindable-js-error.html
          'Can\'t find variable: ZiteReader',
          'jigsaw is not defined',
          'ComboSearch is not defined',
 
          // ISP injections
          'atomicFindClose',
 
          // Harmless errors
          'ResizeObserver loop limit exceeded'
        ],
 
        // Ignore errors from specific URLs
        denyUrls: [
          // Chrome extensions
          /extensions\//i,
          /^chrome:\/\//i,
          /^chrome-extension:\/\//i,
 
          // Facebook
          /graph\.facebook\.com/i,
          /connect\.facebook\.net\/en_US\/all\.js/i,
 
          // Other plugins
          /eatdifferent\.com\.woopra-ns\.com/i,
          /static\.woopra\.com\/js\/woopra\.js/i
        ]
      });
 
      this.enabled = true;
      console.log('Sentry monitoring initialized');
 
    } catch (error) {
      console.error('Failed to initialize Sentry:', error);
    }
  }
 
  // Get Sentry DSN from meta tag or environment
  getDSN() {
    // Try meta tag first (for static sites)
    const metaTag = document.querySelector('meta[name="sentry-dsn"]');
    if (metaTag) {
      return metaTag.content;
    }
 
    // For build-time replacement, you can use process.env
    // This requires a bundler plugin to replace process.env.SENTRY_DSN
    // with the actual value during build
    Iif (typeof process !== 'undefined' && process.env && process.env.SENTRY_DSN) {
      return process.env.SENTRY_DSN;
    }
 
    return null;
  }
 
  // Determine environment from hostname
  getEnvironment() {
    const hostname = window.location.hostname;
 
    if (hostname === 'localhost' || hostname === '127.0.0.1') {
      return 'development';
    } else if (hostname.includes('.github.io')) {
      return 'production';
    } else if (hostname.includes('underafter.com')) {
      return 'production';
    } else {
      return 'staging';
    }
  }
 
  // Get release version from meta tag or package.json version
  getRelease() {
    const metaTag = document.querySelector('meta[name="version"]');
    if (metaTag) {
      return `underafter@${metaTag.content}`;
    }
 
    // Default release identifier
    return 'underafter@unknown';
  }
 
  // Capture custom error
  captureError(error, context = {}) {
    if (!this.enabled) return;
 
    Sentry.captureException(error, {
      extra: context
    });
  }
 
  // Capture custom message
  captureMessage(message, level = 'info', context = {}) {
    if (!this.enabled) return;
 
    Sentry.captureMessage(message, {
      level: level,
      extra: context
    });
  }
 
  // Set user context
  setUser(user) {
    if (!this.enabled) return;
 
    Sentry.setUser(user);
  }
 
  // Add breadcrumb (for debugging context)
  addBreadcrumb(breadcrumb) {
    if (!this.enabled) return;
 
    Sentry.addBreadcrumb(breadcrumb);
  }
}