{"version":3,"file":"PageViewTracker.mjs","sources":["../../../src/trackers/PageViewTracker.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { ConsoleLogger } from '@aws-amplify/core';\nimport { isBrowser } from '@aws-amplify/core/internals/utils';\nconst logger = new ConsoleLogger('PageViewTracker');\nconst DEFAULT_EVENT_NAME = 'pageView';\nconst DEFAULT_APP_TYPE = 'singlePage';\nconst DEFAULT_URL_PROVIDER = () => {\n    return window.location.origin + window.location.pathname;\n};\nconst PREV_URL_STORAGE_KEY = 'aws-amplify-analytics-prevUrl';\nexport class PageViewTracker {\n    constructor(eventRecorder, options) {\n        this.options = {};\n        this.trackerActive = true;\n        this.eventRecorder = eventRecorder;\n        this.spaTrackingActive = false;\n        this.handleLocationChange = this.handleLocationChange.bind(this);\n        this.configure(eventRecorder, options);\n    }\n    configure(eventRecorder, options) {\n        this.eventRecorder = eventRecorder;\n        // Clean up any existing listeners\n        this.cleanup();\n        // Apply defaults\n        this.options = {\n            appType: options?.appType ?? DEFAULT_APP_TYPE,\n            attributes: options?.attributes ?? undefined,\n            eventName: options?.eventName ?? DEFAULT_EVENT_NAME,\n            urlProvider: options?.urlProvider ?? DEFAULT_URL_PROVIDER,\n        };\n        // Configure SPA or MPA page view tracking\n        if (isBrowser()) {\n            if (this.options.appType === 'singlePage') {\n                this.setupSPATracking();\n            }\n            else {\n                this.setupMPATracking();\n            }\n            this.trackerActive = true;\n        }\n    }\n    cleanup() {\n        // No-op if document listener is not active\n        if (!this.trackerActive) {\n            return;\n        }\n        // Clean up SPA page view listeners\n        if (this.spaTrackingActive) {\n            window.history.pushState = this.originalPushState;\n            window.history.replaceState = this.originalReplaceState;\n            this.pushStateProxy?.revoke();\n            this.replaceStateProxy?.revoke();\n            window.removeEventListener('popstate', this.handleLocationChange);\n            this.spaTrackingActive = false;\n        }\n    }\n    setupSPATracking() {\n        if (!this.spaTrackingActive) {\n            // Configure proxies on History APIs\n            this.pushStateProxy = Proxy.revocable(window.history.pushState, {\n                apply: (target, thisArg, args) => {\n                    target.apply(thisArg, args);\n                    this.handleLocationChange();\n                },\n            });\n            this.replaceStateProxy = Proxy.revocable(window.history.replaceState, {\n                apply: (target, thisArg, args) => {\n                    target.apply(thisArg, args);\n                    this.handleLocationChange();\n                },\n            });\n            this.originalPushState = window.history.pushState;\n            this.originalReplaceState = window.history.replaceState;\n            window.history.pushState = this.pushStateProxy.proxy;\n            window.history.replaceState = this.replaceStateProxy.proxy;\n            window.addEventListener('popstate', this.handleLocationChange);\n            sessionStorage.removeItem(PREV_URL_STORAGE_KEY);\n            this.spaTrackingActive = true;\n        }\n    }\n    setupMPATracking() {\n        this.handleLocationChange();\n    }\n    handleLocationChange() {\n        const currentUrl = this.options.urlProvider();\n        const eventName = this.options.eventName || DEFAULT_EVENT_NAME;\n        if (this.urlHasChanged()) {\n            sessionStorage.setItem(PREV_URL_STORAGE_KEY, currentUrl);\n            // Assemble attribute list\n            const attributes = Object.assign({\n                url: currentUrl,\n            }, this.options.attributes);\n            logger.debug('Recording automatically tracked page view event', {\n                eventName,\n                attributes,\n            });\n            this.eventRecorder(eventName, attributes);\n        }\n    }\n    urlHasChanged() {\n        const prevUrl = sessionStorage.getItem(PREV_URL_STORAGE_KEY);\n        const currUrl = this.options.urlProvider();\n        return currUrl !== prevUrl;\n    }\n}\n"],"names":[],"mappings":";;;AAAA;AACA;AAGA,MAAM,MAAM,GAAG,IAAI,aAAa,CAAC,iBAAiB,CAAC;AACnD,MAAM,kBAAkB,GAAG,UAAU;AACrC,MAAM,gBAAgB,GAAG,YAAY;AACrC,MAAM,oBAAoB,GAAG,MAAM;AACnC,IAAI,OAAO,MAAM,CAAC,QAAQ,CAAC,MAAM,GAAG,MAAM,CAAC,QAAQ,CAAC,QAAQ;AAC5D,CAAC;AACD,MAAM,oBAAoB,GAAG,+BAA+B;AACrD,MAAM,eAAe,CAAC;AAC7B,IAAI,WAAW,CAAC,aAAa,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