Personally, I wouldn't depend on "Sitecore.Xdb.Configuration.XdbSettings.Enabled" setting. From (I believe) Sitecore 9.1 and onwards, Sitecore delivered XM packages and XP packages. Although XP can run in XM mode, it is still an XP instance. The XM package does not contain any specific XP assemblies, such as the Sitecore.Analytics assembly. This means that the following code probably doesn't work, as the xDB assembly isn't available at all in the XM package:
Sitecore.Xdb.Configuration.XdbSettings.Enabled
To overcome this, you could explicitly check if certain "XP specific assemblies" have been loaded or not, just by executing this logic:
var isXP = ContainsAssembly("Sitecore.Analytics");
private bool ContainsAssembly(string name)
{
bool assemblyLoaded = false;
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (var assembly in assemblies)
{
var assemblyName = assembly.GetName();
if (assemblyName.Name == name)
{
assemblyLoaded = true;
break;
}
}
return assemblyLoaded;
}
if the assembly is not in the app domain, you can be pretty sure that you are running an XM instance (and thus, XM mode). IF it is available, you can run extra checks, to see if you are running XP or XM mode in an XP instance