public Analytics(string apiSecret, UserInfo userInfo, Dictionary<string, string> propertiesThatGoWithEveryEvent, bool allowTracking = true)
{
if (_singleton != null)
{
throw new ApplicationException("You can only construct a single Analytics object.");
}
_singleton = this;
_propertiesThatGoWithEveryEvent = propertiesThatGoWithEveryEvent;
_userInfo = userInfo;
AllowTracking = allowTracking;
//UrlThatReturnsExternalIpAddress is a static and should really be set before this is called, so don't mess with it if the client has given us a different url to us
if (string.IsNullOrEmpty(UrlThatReturnsExternalIpAddress))
UrlThatReturnsExternalIpAddress = "http://icanhazip.com"; //went down: "http://ipecho.net/plain";
if (!AllowTracking)
return;
//bring in settings from any previous version
if (AnalyticsSettings.Default.NeedUpgrade)
{
//see http://stackoverflow.com/questions/3498561/net-applicationsettingsbase-should-i-call-upgrade-every-time-i-load
AnalyticsSettings.Default.Upgrade();
AnalyticsSettings.Default.NeedUpgrade = false;
AnalyticsSettings.Default.Save();
}
if (string.IsNullOrEmpty(AnalyticsSettings.Default.IdForAnalytics))
{
// Apparently a first-time install. Any chance we can migrate settings from another channel of this app?
// We really want to use the same ID if possible to keep our statistics valid.
try
{
AttemptToGetUserIdSettingsFromDifferentChannel();
}
catch (Exception)
{
// Oh, well, we tried.
}
}
Segment.Analytics.Initialize(apiSecret);
Segment.Analytics.Client.Failed += Client_Failed;
Segment.Analytics.Client.Succeeded += Client_Succeeded;
if (string.IsNullOrEmpty(AnalyticsSettings.Default.IdForAnalytics))
{
AnalyticsSettings.Default.IdForAnalytics = Guid.NewGuid().ToString();
AnalyticsSettings.Default.Save();
}
var context = new Context();
context.Add("language", _userInfo.UILanguageCode);
_options = new Options();
_options.SetContext(context);
UpdateSegmentIOInformationOnThisUser();
ReportIpAddressOfThisMachineAsync(); //this will take a while and may fail, so just do it when/if we can
string versionNumberWithBuild = "";
try
{
versionNumberWithBuild = System.Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString();
}
catch (NullReferenceException)
{
try
{
// GetEntryAssembly is null for MAF plugins
versionNumberWithBuild = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
}
catch (NullReferenceException)
{
// This probably can't happen, but if it does, just roll with it.
}
}
string versionNumber = versionNumberWithBuild.Split('.').Take(2).Aggregate((a, b) => a + "." + b);
SetApplicationProperty("Version", versionNumber);
SetApplicationProperty("FullVersion", versionNumberWithBuild);
SetApplicationProperty("UserName", GetUserNameForEvent());
SetApplicationProperty("Browser", GetOperatingSystemLabel());
if (string.IsNullOrEmpty(AnalyticsSettings.Default.LastVersionLaunched))
{
//"Created" is a special property that segment.io understands and coverts to equivalents in various analytics services
//So it's not as descriptive for us as "FirstLaunchOnSystem", but it will give the best experience on the analytics sites.
TrackWithApplicationProperties("Created");
}
else if (AnalyticsSettings.Default.LastVersionLaunched != versionNumberWithBuild)
{
TrackWithApplicationProperties("Upgrade", new Properties
{
{"OldVersion", AnalyticsSettings.Default.LastVersionLaunched},
});
}
// We want to record the launch event independent of whether we also recorded a special first launch
// But that is done after we retrieve (or fail to retrieve) our external ip address.
// See http://issues.bloomlibrary.org/youtrack/issue/BL-4011.
AnalyticsSettings.Default.LastVersionLaunched = versionNumberWithBuild;
AnalyticsSettings.Default.Save();
}