Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix compatibility with matomo 4 #76 #79

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Piwik.Tracker.Tests/PiwikTrackerTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Web;
Expand All @@ -24,6 +25,37 @@ public void SetUpTest()
_sut = new PiwikTracker(SiteId, PiwikBaseUrl);
}

[Test]
public void GetVisitorId_TwoComponentCookie_ReturnsCookieId()
{
//Arrange
HttpContext.Current = new HttpContext(new HttpRequest("filename", "http://url.com", "query"), new HttpResponse(new StringWriter()));
var cookies = HttpContext.Current.Request.Cookies;
cookies.Add(new HttpCookie("_pk_id.1.4ea4", "d32ffdf363c2f313.1610980363."));

//Act
var actual = _sut.GetVisitorId();

//Assert
Assert.That(actual, Is.Not.Null.Or.Empty);
Assert.That(actual, Is.EqualTo("d32ffdf363c2f313"));
}

[Test]
public void GetRequest_TwoComponentCookie_SetsCorrectCookie()
{
//Arrange
HttpContext.Current = new HttpContext(new HttpRequest("filename", "http://url.com", "query"), new HttpResponse(new StringWriter()));
var cookies = HttpContext.Current.Request.Cookies;
cookies.Add(new HttpCookie("_pk_id.1.4ea4", "d32ffdf363c2f313.1610980363."));

//Act
var request = _sut.GetRequest(SiteId);

//Assert
Assert.That(request, Does.Contain("&_id=d32ffdf363c2f313"));
}

[Test]
[TestCase(Scopes.Page, 2)]
[TestCase(Scopes.Event, 3)]
Expand Down
25 changes: 2 additions & 23 deletions Piwik.Tracker/PiwikTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,6 @@ public class PiwikTracker
private string _configCookieDomain = "";
private readonly long _currentTs = (long)(DateTime.UtcNow - DateTimeUtils.UnixEpoch).TotalSeconds;
private long _createTs;
private long? _visitCount = 0;
private long? _currentVisitTs;
private long? _lastVisitTs;
private long? _lastEcommerceOrderTs;
private bool _sendImageResponse = true;

/// <summary>
Expand Down Expand Up @@ -1246,20 +1242,7 @@ protected bool LoadVisitorIdCookie()
}
_cookieVisitorId = parts[0]; // provides backward compatibility since getVisitorId() didn't change any existing VisitorId value
_createTs = long.Parse(parts[1]);
if (!string.IsNullOrWhiteSpace(parts[2]))
{
_visitCount = long.Parse(parts[2]);
}
// _currentVisitTs is set for information / debugging purposes
_currentVisitTs = long.Parse(parts[3]);
if (!string.IsNullOrWhiteSpace(parts[4]))
{
_lastVisitTs = long.Parse(parts[4]);
}
if (!string.IsNullOrWhiteSpace(parts[5]))
{
_lastEcommerceOrderTs = long.Parse(parts[5]);
}

return true;
}

Expand Down Expand Up @@ -1511,9 +1494,6 @@ internal string GetRequest(int idSite)

// Values collected from cookie
"&_idts=" + _createTs +
"&_idvc=" + _visitCount +
((_lastVisitTs != null) ? "&_viewts=" + _lastVisitTs : "") +
((_lastEcommerceOrderTs != null) ? "&_ects=" + _lastEcommerceOrderTs : "") +

// These parameters are set by the JS, but optional when using API
(!string.IsNullOrEmpty(_plugins) ? _plugins : "") +
Expand Down Expand Up @@ -1681,8 +1661,7 @@ protected void SetFirstPartyCookies()
SetCookie("ses", "*", ConfigSessionCookieTimeout);

// Set the 'id' cookie
var visitCount = _visitCount + 1;
var cookieValue = GetVisitorId() + "." + _createTs + "." + visitCount + "." + _currentTs + "." + _lastVisitTs + "." + _lastEcommerceOrderTs;
var cookieValue = GetVisitorId() + "." + _createTs;
SetCookie("id", cookieValue, ConfigVisitorCookieTimeout);

// Set the 'cvar' cookie
Expand Down