// If file is copied from node_modules/@rudderstack/analytics-js-service-worker/npm/esm/index.js in extension resources folder
import{Analytics}from"./rudderAnalytics.js";// If the package is imported directly as umd and then bundled in the background script
import{Analytics}from"@rudderstack/analytics-js-service-worker/umd/index.js";// If the package is imported directly as es-module and then bundled in the background script
import{Analytics}from"@rudderstack/analytics-js-service-worker";
See the sample application for more information on using the service worker NPM package as a background script.
Content script
To use the JavaScript SDK as a content script, place it in your Chrome extension resources by following either of these approaches:
Copy the index.js file from the node_modules folder after installing the package (node_modules/@rudderstack/analytics-js/dist/npm/modern/cjs/index.js) and place it as a part of the resources.
You can use the cjs bundle from legacy folder (node_modules/@rudderstack/analytics-js/dist/npm/legacy/cjs/index.js) as well, depending on your requirement.
Note that the modern folder is targeted for browsers that support the modern JavaScript syntax, especially dynamic imports, whereas the legacy folder is meant for the rest of the browsers.
Use a JS bundler and bundle it as part of your content script.
You need to enable relevant permissions in the manifest file according to the required capabilities and allowed connections.
You can also react to events available in both the content and background scripts by leveraging the Chrome API.
The following sample scripts help you track any URL changes:
// Prepend the JavaScript SDK file here
rudderanalytics.load("<WRITE_KEY>","<DATA_PLANE_URL>");chrome.runtime.onMessage.addListener((obj,sender,response)=>{const{type,value}=obj;if(type==="trackURL"){rudderanalytics.track("URL change",{url:value});}});
This site uses cookies to improve your experience while you navigate through the website. Out of
these
cookies, the cookies that are categorized as necessary are stored on your browser as they are as
essential
for the working of basic functionalities of the website. We also use third-party cookies that
help
us
analyze and understand how you use this website. These cookies will be stored in your browser
only
with
your
consent. You also have the option to opt-out of these cookies. But opting out of some of these
cookies
may
have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This
category only includes cookies that ensures basic functionalities and security
features of the website. These cookies do not store any personal information.
This site uses cookies to improve your experience. If you want to
learn more about cookies and why we use them, visit our cookie
policy. We'll assume you're ok with this, but you can opt-out if you wish Cookie Settings.