/// <summary> /// Builds a PiwikTracker object, used to track visits, pages and Goal conversions /// for a specific website, by using the Piwik Tracking API. /// /// If the tracker is used within a web page or web controller, the following information are pre-initialised : /// URL Referrer, current page URL, remote IP, Accept-Language HTTP header and User-Agent HTTP header. /// </summary> /// <param name="idSite">Id site to be tracked</param> /// <param name="apiUrl">"http://example.org/piwik/" or "http://piwik.example.org/". If set, will overwrite PiwikTracker.URL</param> public PiwikTracker(int idSite, string apiUrl = null) { this.cookieSupport = false; this.userAgent = null; this.localTime = DateTimeOffset.MinValue; this.hasCookies = false; this.plugins = null; this.visitorCustomVar = new Dictionary <string, string[]>(); this.pageCustomVar = new Dictionary <string, string[]>(); this.customData = null; this.forcedDatetime = DateTimeOffset.MinValue; this.token_auth = null; this.attributionInfo = null; this.ecommerceLastOrderTimestamp = DateTimeOffset.MinValue; this.ecommerceItems = new Dictionary <string, object[]>(); this.requestCookie = null; this.idSite = idSite; var currentContext = HttpContext.Current; if (currentContext != null) { if (currentContext.Request.UrlReferrer != null) { this.urlReferrer = currentContext.Request.UrlReferrer.AbsoluteUri; } this.ip = currentContext.Request.UserHostAddress; if (currentContext.Request.UserLanguages != null && currentContext.Request.UserLanguages.Any()) { this.acceptLanguage = currentContext.Request.UserLanguages.First(); } this.userAgent = currentContext.Request.UserAgent; } this.pageUrl = getCurrentUrl(); if (!String.IsNullOrEmpty(apiUrl)) { URL = apiUrl; } this.setNewVisitorId(); // Allow debug while blocking the request this.requestTimeout = 600; this.doBulkRequests = false; this.storedTrackingActions = new List <string>(); }
/// <summary> /// Returns the currently assigned Attribution Information stored in a first party cookie. /// /// This function will only work if the user is initiating the current request, and his cookies /// can be read from an active HttpContext. /// </summary> /// <returns>Referer information for Goal conversion attribution</returns> public AttributionInfo getAttributionInfo() { HttpCookie refCookie = getCookieMatchingName("ref." + idSite + "."); if (refCookie == null) { return(null); } string[] cookieDecoded = new JavaScriptSerializer().Deserialize <string[]>(HttpUtility.UrlDecode(refCookie.Value)); if (cookieDecoded == null) { return(null); } int arraySize = cookieDecoded.Length; if (arraySize == 0) { return(null); } AttributionInfo attributionInfo = new AttributionInfo(); if (!String.IsNullOrEmpty(cookieDecoded[0])) { attributionInfo.campaignName = cookieDecoded[0]; } if (arraySize > 1 && !String.IsNullOrEmpty(cookieDecoded[1])) { attributionInfo.campaignKeyword = cookieDecoded[1]; } if (arraySize > 2 && !String.IsNullOrEmpty(cookieDecoded[2])) { attributionInfo.referrerTimestamp = new DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(Convert.ToInt32(cookieDecoded[2])); } if (arraySize > 3 && !String.IsNullOrEmpty(cookieDecoded[3])) { attributionInfo.referrerUrl = cookieDecoded[3]; } return(attributionInfo); }
/// <summary> /// Sets the attribution information to the visit, so that subsequent Goal conversions are /// properly attributed to the right Referrer URL, timestamp, Campaign Name & Keyword. /// </summary> /// <param name="attributionInfo">Attribution info for the visit</param> public void setAttributionInfo(AttributionInfo attributionInfo) { this.attributionInfo = attributionInfo; }
/// <summary> /// Returns the currently assigned Attribution Information stored in a first party cookie. /// /// This function will only work if the user is initiating the current request, and his cookies /// can be read from an active HttpContext. /// </summary> /// <returns>Referer information for Goal conversion attribution</returns> public AttributionInfo getAttributionInfo() { HttpCookie refCookie = getCookieMatchingName("ref." + idSite + "."); if(refCookie == null) { return null; } string[] cookieDecoded = new JavaScriptSerializer().Deserialize<string[]>(HttpUtility.UrlDecode(refCookie.Value)); if(cookieDecoded == null) { return null; } int arraySize = cookieDecoded.Length; if(arraySize == 0) { return null; } AttributionInfo attributionInfo = new AttributionInfo(); if(!String.IsNullOrEmpty(cookieDecoded[0])) { attributionInfo.campaignName = cookieDecoded[0]; } if(arraySize > 1 && !String.IsNullOrEmpty(cookieDecoded[1])) { attributionInfo.campaignKeyword = cookieDecoded[1]; } if(arraySize > 2 && !String.IsNullOrEmpty(cookieDecoded[2])) { attributionInfo.referrerTimestamp = new DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(Convert.ToInt32(cookieDecoded[2])); } if(arraySize > 3 && !String.IsNullOrEmpty(cookieDecoded[3])) { attributionInfo.referrerUrl = cookieDecoded[3]; } return attributionInfo; }
/// <summary> /// Builds a PiwikTracker object, used to track visits, pages and Goal conversions /// for a specific website, by using the Piwik Tracking API. /// /// If the tracker is used within a web page or web controller, the following information are pre-initialised : /// URL Referrer, current page URL, remote IP, Accept-Language HTTP header and User-Agent HTTP header. /// </summary> /// <param name="idSite">Id site to be tracked</param> /// <param name="apiUrl">"http://example.org/piwik/" or "http://piwik.example.org/". If set, will overwrite PiwikTracker.URL</param> public PiwikTracker(int idSite, string apiUrl = "") { this.userAgent = null; this.localTime = DateTimeOffset.MinValue; this.hasCookies = false; this.plugins = null; this.pageCustomVar = new Dictionary<string, string[]>(); this.eventCustomVar = new Dictionary<string, string[]>(); this.customData = null; this.forcedDatetime = DateTimeOffset.MinValue; this.forcedNewVisit = false; this.token_auth = null; this.attributionInfo = null; this.ecommerceLastOrderTimestamp = DateTimeOffset.MinValue; this.ecommerceItems = new Dictionary<string, object[]>(); this.generationTime = null; this.idSite = idSite; var currentContext = HttpContext.Current; if (currentContext != null) { if (currentContext.Request.UrlReferrer != null) { this.urlReferrer = currentContext.Request.UrlReferrer.AbsoluteUri; } this.ip = currentContext.Request.UserHostAddress; if (currentContext.Request.UserLanguages != null && currentContext.Request.UserLanguages.Any()) this.acceptLanguage = currentContext.Request.UserLanguages.First(); this.userAgent = currentContext.Request.UserAgent; } this.pageCharset = DEFAULT_CHARSET_PARAMETER_VALUES; this.pageUrl = getCurrentUrl(); if (!String.IsNullOrEmpty(apiUrl)) { URL = apiUrl; } // Life of the visitor cookie (in sec) this.configVisitorCookieTimeout = 33955200; // 13 months (365 + 28 days) // Life of the session cookie (in sec) this.configSessionCookieTimeout = 1800; // 30 minutes // Life of the session cookie (in sec) this.configReferralCookieTimeout = 15768000; // 6 months // Visitor Ids in order this.userId = null; this.forcedVisitorId = null; this.cookieVisitorId = null; this.randomVisitorId = null; this.setNewVisitorId(); this.configCookiesDisabled = false; this.configCookiePath = DEFAULT_COOKIE_PATH; this.configCookieDomain = ""; this.currentTs = (long)(DateTime.Now - new DateTime(1970, 1, 1)).TotalSeconds; this.createTs = this.currentTs; this.visitCount = 0; this.currentVisitTs = null; this.lastVisitTs = null; this.lastEcommerceOrderTs = null; // Allow debug while blocking the request this.requestTimeout = 600; this.doBulkRequests = false; this.storedTrackingActions = new List<string>(); this.visitorCustomVar = this.getCustomVariablesFromCookie(); }