diff --git a/Piwik.Tracker.Tests/PiwikTrackerTests.cs b/Piwik.Tracker.Tests/PiwikTrackerTests.cs index e081cd9..e79bb7a 100644 --- a/Piwik.Tracker.Tests/PiwikTrackerTests.cs +++ b/Piwik.Tracker.Tests/PiwikTrackerTests.cs @@ -174,21 +174,17 @@ public void SetAttributionInfo_WhenCampaignNameSpecified_IsAddedToRequest(string [TestCase("")] public void SetAttributionInfo_WhenReferrerTimestampSpecified_IsAddedToRequest(string referrerDateTime) { - if (string.IsNullOrEmpty(referrerDateTime)) + var expectedTs = "1486223506"; // cf http://xmillis.com/l1c9bu4i.9e + var timestampProvided = !string.IsNullOrEmpty(referrerDateTime); + if (timestampProvided) { - // Assert - var actual = _sut.GetRequest(SiteId); - Assert.That(actual, Does.Not.Contain("&_refts=")); - } - else - { - // Arrange, Act var referrerTimestamp = DateTimeOffset.Parse(referrerDateTime, CultureInfo.InvariantCulture); - _sut.SetAttributionInfo(new AttributionInfo { ReferrerTimestamp = referrerTimestamp }); - // Assert - var actual = _sut.GetRequest(SiteId); - Assert.That(actual, Does.Contain("&_refts=" + referrerTimestamp.ToUnixTimeSeconds())); + var attrInfo = new AttributionInfo {ReferrerTimestamp = referrerTimestamp}; + Assert.That(attrInfo.ToArray()[2], Is.EqualTo(expectedTs)); + _sut.SetAttributionInfo(attrInfo); + } + Assert.That(_sut.GetRequest(SiteId), timestampProvided ? Does.Contain("&_refts=" + expectedTs) : Does.Not.Contain("&_refts=")); } [Test] diff --git a/Piwik.Tracker/AttributionInfo.cs b/Piwik.Tracker/AttributionInfo.cs index a92b2f3..3ee59eb 100644 --- a/Piwik.Tracker/AttributionInfo.cs +++ b/Piwik.Tracker/AttributionInfo.cs @@ -38,7 +38,7 @@ public string[] ToArray() var infos = new string[4]; infos[0] = CampaignName; infos[1] = CampaignKeyword; - infos[2] = (ReferrerTimestamp - Constants.UnixEpoch).TotalSeconds.ToString(CultureInfo.InvariantCulture); + infos[2] = DateTimeUtils.ConvertToUnixTime(ReferrerTimestamp); infos[3] = ReferrerUrl; return infos; } diff --git a/Piwik.Tracker/Constants.cs b/Piwik.Tracker/Constants.cs deleted file mode 100644 index 1bedc36..0000000 --- a/Piwik.Tracker/Constants.cs +++ /dev/null @@ -1,12 +0,0 @@ -namespace Piwik.Tracker -{ - using System; - - /// - /// Constants used by Piwik internally. - /// - internal static class Constants - { - public static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); - } -} diff --git a/Piwik.Tracker/DateTimeUtils.cs b/Piwik.Tracker/DateTimeUtils.cs new file mode 100644 index 0000000..7419505 --- /dev/null +++ b/Piwik.Tracker/DateTimeUtils.cs @@ -0,0 +1,16 @@ +using System.Globalization; + +namespace Piwik.Tracker +{ + using System; + + internal static class DateTimeUtils + { + public static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); + + public static string ConvertToUnixTime(DateTimeOffset date) + { + return (date - UnixEpoch).TotalSeconds.ToString(CultureInfo.InvariantCulture); + } + } +} diff --git a/Piwik.Tracker/Piwik.Tracker.csproj b/Piwik.Tracker/Piwik.Tracker.csproj index 9facac8..062c0d9 100644 --- a/Piwik.Tracker/Piwik.Tracker.csproj +++ b/Piwik.Tracker/Piwik.Tracker.csproj @@ -1,65 +1,65 @@ - - - - Debug - AnyCPU - 8.0.30703 - 2.0 - {C3BB0E94-F45C-4691-81FF-061FC20E3209} - Library - Properties - Piwik.Tracker - Piwik.Tracker - v4.0 - 512 - - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - bin\Debug\Piwik.Tracker.xml - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - bin\Release\Piwik.Tracker.XML - - - - - - - - - - - - - - - - - - - - - - - - + + + + Debug + AnyCPU + 8.0.30703 + 2.0 + {C3BB0E94-F45C-4691-81FF-061FC20E3209} + Library + Properties + Piwik.Tracker + Piwik.Tracker + v4.0 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + bin\Debug\Piwik.Tracker.xml + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + bin\Release\Piwik.Tracker.XML + + + + + + + + + + + + + + + + + + + + + + + + + --> \ No newline at end of file diff --git a/Piwik.Tracker/PiwikTracker.cs b/Piwik.Tracker/PiwikTracker.cs index 3ed109f..7c0e41d 100644 --- a/Piwik.Tracker/PiwikTracker.cs +++ b/Piwik.Tracker/PiwikTracker.cs @@ -180,7 +180,7 @@ public class PiwikTracker private bool _configCookiesDisabled; private string _configCookiePath = DefaultCookiePath; private string _configCookieDomain = ""; - private readonly long _currentTs = (long)(DateTime.UtcNow - Constants.UnixEpoch).TotalSeconds; + private readonly long _currentTs = (long)(DateTime.UtcNow - DateTimeUtils.UnixEpoch).TotalSeconds; private long _createTs; private long? _visitCount = 0; private long? _currentVisitTs; @@ -1329,7 +1329,7 @@ public AttributionInfo GetAttributionInfo() if (arraySize > 2 && !string.IsNullOrEmpty(cookieDecoded[2])) { - attributionInfo.ReferrerTimestamp = Constants.UnixEpoch.AddSeconds(Convert.ToInt32(cookieDecoded[2])); + attributionInfo.ReferrerTimestamp = DateTimeUtils.UnixEpoch.AddSeconds(Convert.ToInt32(cookieDecoded[2])); } if (arraySize > 3 && !string.IsNullOrEmpty(cookieDecoded[3])) @@ -1526,7 +1526,7 @@ internal string GetRequest(int idSite) (!_localTime.Equals(DateTimeOffset.MinValue) ? "&h=" + _localTime.Hour + "&m=" + _localTime.Minute + "&s=" + _localTime.Second : "") + ((_width != 0 && _height != 0) ? "&res=" + _width + "x" + _height : "") + (_hasCookies ? "&cookie=1" : "") + - (!_ecommerceLastOrderTimestamp.Equals(DateTimeOffset.MinValue) ? "&_ects=" + FormatTimestamp(_ecommerceLastOrderTimestamp) : "") + + (!_ecommerceLastOrderTimestamp.Equals(DateTimeOffset.MinValue) ? "&_ects=" + DateTimeUtils.ConvertToUnixTime(_ecommerceLastOrderTimestamp) : "") + // Various important attributes // todo _customData is never assigned! @@ -1548,7 +1548,7 @@ internal string GetRequest(int idSite) // Campaign keyword ((_attributionInfo != null && !string.IsNullOrEmpty(_attributionInfo.CampaignKeyword)) ? "&_rck=" + UrlEncode(_attributionInfo.CampaignKeyword) : "") + // Timestamp at which the referrer was set - ((_attributionInfo != null && !_attributionInfo.ReferrerTimestamp.Equals(DateTimeOffset.MinValue)) ? "&_refts=" + FormatTimestamp(_attributionInfo.ReferrerTimestamp) : "") + + ((_attributionInfo != null && !_attributionInfo.ReferrerTimestamp.Equals(DateTimeOffset.MinValue)) ? "&_refts=" + DateTimeUtils.ConvertToUnixTime(_attributionInfo.ReferrerTimestamp) : "") + // Referrer URL ((_attributionInfo != null && !string.IsNullOrEmpty(_attributionInfo.ReferrerUrl)) ? "&_ref=" + UrlEncode(_attributionInfo.ReferrerUrl) : "") + @@ -1709,7 +1709,7 @@ protected void SetCookie(string cookieName, string cookieValue, long cookieTtl) if (HttpContext.Current != null) { var cookieExpire = _currentTs + cookieTtl; - HttpContext.Current.Response.Cookies.Add(new HttpCookie(GetCookieName(cookieName), cookieValue) { Expires = Constants.UnixEpoch.AddSeconds(cookieExpire), Path = _configCookiePath, Domain = _configCookieDomain }); + HttpContext.Current.Response.Cookies.Add(new HttpCookie(GetCookieName(cookieName), cookieValue) { Expires = DateTimeUtils.UnixEpoch.AddSeconds(cookieExpire), Path = _configCookiePath, Domain = _configCookieDomain }); } } @@ -1732,13 +1732,6 @@ private string FormatDateValue(DateTimeOffset date) return date.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss"); } - private string FormatTimestamp(DateTimeOffset date) - { - TimeSpan diff = date - Constants.UnixEpoch; - double seconds = Convert.ToInt32(diff.TotalSeconds); - return seconds.ToString(CultureInfo.InvariantCulture); - } - private string FormatMonetaryValue(double value) { return value.ToString("0.##", new CultureInfo("en-US"));