17

I am trying to detect whether my extension was uninstalled.

I can't use chrome.management.onUninstalled because it will be fired on other extension.

6 Answers 6

31

As of Chrome 41, you can now open a URL when the extension is uninstalled. That could contain an exit survey or track the uninstall event as some sort of analytics.

2
  • 4
    It would be helpful if you highlighted that extension code still can't detect / react to being uninstalled.
    – Xan
    Apr 2, 2015 at 20:34
  • 3
    You can send details of the user stored locally on the device as URL parameters. If you have a server, you can use this to send the details and track who has uninstalled
    – Gibolt
    Aug 24, 2015 at 0:03
13

Google Chrome, unlike Firefox, doesn’t allow to detect when the user uninstalls the extension, which is quite useful to understand user behaviour. There is a feature request on crbug.com with a discussion of this feature but it has not been implemented yet.

4
  • I think this is the one which is being discussed regarding uninstall of extension.
    – vikiiii
    Oct 17, 2011 at 11:22
  • 2
    What i do is basically set up an event on blur for my orphaned content script to get a 1pixel image on interval and if the window is blured and it can't get the image it's because the extension isn't installed i then reload the page. It's jank but it works.
    – btm1
    May 21, 2014 at 20:17
  • 1
    @btm1, Please re-explain.
    – Pacerier
    Aug 7, 2017 at 5:47
  • If you are looking for this in 2023, there is an API now setUninstallURL - developer.chrome.com/docs/extensions/reference/runtime/…
    – Richard
    May 27 at 6:38
8

You can call chrome.runtime.setUninstallURL("www.example.com/survey") and redirect user to a url. Unfortunately, as soon as the extension is removed, the background script is removed too, and you can't do anything like log event or send hit to google analytics.

What I did is to set the redirect url to my server endpoint, and do some tasks like logging event to my own db, or sending hit to google analytics (ga hit builder). Then call res.status(301).redirect("www.example.com/survey") to some survey url. Finally I can send the uninstall event to google analysis.

1

If you're on Manifest V3, you can add it on your onInstalled Listener. If you want to capture uninstall for existing users as well, you need to add it to 'update' as well.

Place this code in your background page:

chrome.runtime.onInstalled.addListener(function (details) {
    if (details.reason == 'install') {
        ... can add things like sending a user to a tutorial page on your website
        chrome.runtime.setUninstallURL('https://www.yourwebsite.com/uninstall');
    } else if (details.reason == 'update') {
        ... can add things like sending user to a update page on your website
        chrome.runtime.setUninstallURL('https://www.yourwebsite.com/uninstall');
    }
});

Find more information here: https://developer.chrome.com/docs/extensions/reference/runtime/#method-setUninstallURL

-1

For mv3: An easy way would be to have

// Redirect users to a form when the extension is uninstalled.
const uninstallListener = (details) => {
  if (details.reason === chrome.runtime.OnInstalledReason.INSTALL) {
    chrome.runtime.setUninstallURL('https://forms.gle/...');
  }

  if (details.reason === chrome.runtime.OnInstalledReason.UPDATE) {
    // TODO: show changelog
  }
};
chrome.runtime.onInstalled.addListener(uninstallListener);

Place it in your background.

-1

Content Script can Detect an Uninstall

Simply check the value of chrome.runtime, which becomes undefined when an extension is uninstalled.

A good trigger to check this is port disconnect:

// content_script.js

const port = chrome.runtime.connect();

port.onDisconnect.addListener(onPortDisconnect);

function onPortDisconnect() {
  // After the extension is disabled/uninstalled, `chrome.runtime` may take 
  // a few milliseconds to get cleared, so use a delay before checking.
  setTimeout(() => {
    if (!chrome.runtime?.id) {
      console.log('Extension disabled!');
    }
  }, 1000);
};

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.