/// <summary> /// Track a page view /// </summary> /// <param name="httpContext">Current http context to extract payload from</param> /// <param name="pageTitle">The title of the page / document</param> /// <param name="pageUrl">The path portion of the page URL. Should begin with '/'</param> /// <param name="hostName">Specifies the hostname from which content was hosted</param> /// <param name="uaclient">Client override object</param> public void TrackPageView(HttpContextBase httpContext, string pageTitle, string pageUrl, string hostName, UAClient uaclient = null) { // check for empty page url if (string.IsNullOrWhiteSpace(pageUrl)) { throw new ArgumentNullException("pageUrl", "Google requires a valid URL to process page views."); } // initialize the uaclient if null if (uaclient == null) { uaclient = new UAClient(); } // build the payload StringBuilder data = BuildPayload(BuildBasePayload(httpContext, uaclient), BuildPageTrackPayload(pageTitle, pageUrl, hostName)); // build the http request HttpWebRequest request = BuildRequest(data); // send sync and fail silently try { using (WebResponse response = request.GetResponse()) { } } catch { } }
/// <summary> /// Track a page view /// </summary> /// <param name="httpContext">Current http context to extract payload from</param> /// <param name="pageTitle">The title of the page / document</param> /// <param name="uaclient">Client override object</param> public void TrackPageView(HttpContextBase httpContext, string pageTitle, UAClient uaclient = null) { // check for null context if (httpContext == null) { throw new ArgumentNullException("httpContext", "Null HttpContextBase, Google requires a valid URL to process page views."); } // upfill and build TrackPageView(httpContext, pageTitle, (httpContext != null && httpContext.Request != null && httpContext.Request.Url != null) ? httpContext.Request.Url.PathAndQuery : null, uaclient); }
/// <summary> /// Async track a page view (non-blocking) /// </summary> /// <param name="httpContext">Current http context to extract payload from</param> /// <param name="uaclient">Client override object</param> public void TrackPageViewAsync(HttpContextBase httpContext, UAClient uaclient = null) { // check for null context if (httpContext == null) { throw new ArgumentNullException("httpContext", "Null HttpContextBase, Google requires a valid URL to process page views."); } // upfill and build TrackPageViewAsync(httpContext, null, uaclient); }
/// <summary> /// Track a page view /// </summary> /// <param name="pageTitle">The title of the page / document</param> /// <param name="pageUrl">The path portion of the page URL. Should begin with '/'</param> /// <param name="hostName">Specifies the hostname from which content was hosted</param> /// <param name="uaclient">Client override object</param> public void TrackPageView(string pageTitle, string pageUrl, string hostName, UAClient uaclient = null) { // check for empty page url if (string.IsNullOrWhiteSpace(pageUrl)) { throw new ArgumentNullException("pageUrl", "Google requires a valid URL to process page views."); } // upfill and build TrackPageView((IsHttpRequestAvailable) ? new HttpContextWrapper(HttpContext.Current) : null, pageTitle, pageUrl, hostName, uaclient); }
/// <summary> /// Async track a page view (non-blocking) /// </summary> /// <param name="uaclient">Client override object</param> public void TrackPageViewAsync(UAClient uaclient = null) { // make sure we don't need it passed in if (IsHttpRequestAvailable) { // upfill and build TrackPageViewAsync(new HttpContextWrapper(HttpContext.Current), null, uaclient); } else { // google doesnt accept page views without a url throw new InvalidOperationException("Unable to find a valid HttpContext.Current, Google requires a valid URL to process page views."); } }
/// <summary> /// Async track a page view (non-blocking) /// </summary> /// <param name="pageTitle">The title of the page / document</param> /// <param name="pageUrl">The path portion of the page URL. Should begin with '/'</param> /// <param name="uaclient">Client override object</param> public void TrackPageViewAsync(string pageTitle, string pageUrl, UAClient uaclient = null) { // check for empty page url if (string.IsNullOrWhiteSpace(pageUrl)) { throw new ArgumentNullException("pageUrl", "Google requires a valid URL to process page views."); } // extract the context HttpContextBase httpContext = (IsHttpRequestAvailable) ? new HttpContextWrapper(HttpContext.Current) : null; // upfill and build TrackPageViewAsync(httpContext, pageTitle, pageUrl, (httpContext != null && httpContext.Request != null && httpContext.Request.Url != null) ? httpContext.Request.Url.Host : null, uaclient); }
/// <summary> /// Async track an event (non-blocking) /// </summary> /// <param name="httpContext">Current http context to extract payload from</param> /// <param name="eventCategory">Specifies the event category. Must not be empty</param> /// <param name="eventAction">Specifies the event action. Must not be empty</param> /// <param name="eventLabel">Specifies the event label</param> /// <param name="eventValue">Specifies the event value. Values must be non-negative</param> /// <param name="uaclient">Client override object</param> public void TrackEventAsync(HttpContextBase httpContext, string eventCategory, string eventAction, string eventLabel, int eventValue, UAClient uaclient = null) { // initialize the uaclient if null if (uaclient == null) { uaclient = new UAClient(); } // build the payload StringBuilder data = BuildPayload(BuildBasePayload(httpContext, uaclient), BuildEventTrackPayload(eventCategory, eventAction, eventLabel, eventValue)); // build the http request HttpWebRequest request = BuildRequest(data); // send it async ProcessAsync(request); }
/// <summary> /// Async track a page view (non-blocking) /// </summary> /// <param name="pageTitle">The title of the page / document</param> /// <param name="uaclient">Client override object</param> public void TrackPageViewAsync(string pageTitle, UAClient uaclient = null) { // make sure we don't need it passed in if (IsHttpRequestAvailable) { // extract the context HttpContextBase httpContext = new HttpContextWrapper(HttpContext.Current); // upfill and build TrackPageViewAsync(httpContext, pageTitle, (httpContext != null && httpContext.Request != null && httpContext.Request.Url != null) ? httpContext.Request.Url.PathAndQuery : null, uaclient); } else { // google doesnt accept page views without a url throw new InvalidOperationException("Unable to find a valid HttpContext.Current, Google requires a valid URL to process page views."); } }
/// <summary> /// Track an event /// </summary> /// <param name="httpContext">Current http context to extract payload from</param> /// <param name="eventCategory">Specifies the event category. Must not be empty</param> /// <param name="eventAction">Specifies the event action. Must not be empty</param> /// <param name="eventLabel">Specifies the event label</param> /// <param name="eventValue">Specifies the event value. Values must be non-negative</param> /// <param name="uaclient">Client override object</param> public void TrackEvent(HttpContextBase httpContext, string eventCategory, string eventAction, string eventLabel, int eventValue, UAClient uaclient = null) { // initialize the uaclient if null if (uaclient == null) { uaclient = new UAClient(); } // build the payload StringBuilder data = BuildPayload(BuildBasePayload(httpContext, uaclient), BuildEventTrackPayload(eventCategory, eventAction, eventLabel, eventValue)); // build the http request HttpWebRequest request = BuildRequest(data); // send sync and fail silently try { using (WebResponse response = request.GetResponse()) { } } catch { } }
/// <summary> /// Async track a page view (non-blocking) /// </summary> /// <param name="httpContext">Current http context to extract payload from</param> /// <param name="pageTitle">The title of the page / document</param> /// <param name="uaclient">Client override object</param> public void TrackPageViewAsync(HttpContextBase httpContext, string pageTitle, UAClient uaclient = null) { // check for null context if (httpContext == null) throw new ArgumentNullException("httpContext", "Null HttpContextBase, Google requires a valid URL to process page views."); // upfill and build TrackPageViewAsync(httpContext, pageTitle, (httpContext != null && httpContext.Request != null && httpContext.Request.Url != null) ? httpContext.Request.Url.PathAndQuery : null, uaclient); }
/// <summary> /// Async track a page view (non-blocking) /// </summary> /// <param name="httpContext">Current http context to extract payload from</param> /// <param name="uaclient">Client override object</param> public void TrackPageViewAsync(HttpContextBase httpContext, UAClient uaclient = null) { // check for null context if (httpContext == null) throw new ArgumentNullException("httpContext", "Null HttpContextBase, Google requires a valid URL to process page views."); // upfill and build TrackPageViewAsync(httpContext, null, uaclient); }
/// <summary> /// Async track an event (non-blocking) /// </summary> /// <param name="httpContext">Current http context to extract payload from</param> /// <param name="eventCategory">Specifies the event category. Must not be empty</param> /// <param name="eventAction">Specifies the event action. Must not be empty</param> /// <param name="eventLabel">Specifies the event label</param> /// <param name="eventValue">Specifies the event value. Values must be non-negative</param> /// <param name="uaclient">Client override object</param> public void TrackEventAsync(HttpContextBase httpContext, string eventCategory, string eventAction, string eventLabel, int eventValue, UAClient uaclient = null) { // initialize the uaclient if null if (uaclient == null) uaclient = new UAClient(); // build the payload StringBuilder data = BuildPayload(BuildBasePayload(httpContext, uaclient), BuildEventTrackPayload(eventCategory, eventAction, eventLabel, eventValue)); dataQueue.EnqueueAsync(data); }
/// <summary> /// Async track an event (non-blocking) /// </summary> /// <param name="eventCategory">Specifies the event category. Must not be empty</param> /// <param name="eventAction">Specifies the event action. Must not be empty</param> /// <param name="eventLabel">Specifies the event label</param> /// <param name="eventValue">Specifies the event value. Values must be non-negative</param> /// <param name="uaclient">Client override object</param> public void TrackEventAsync(string eventCategory, string eventAction, string eventLabel, int eventValue, UAClient uaclient = null) { // upfill and build TrackEventAsync((IsHttpRequestAvailable) ? new HttpContextWrapper(HttpContext.Current) : null, eventCategory, eventAction, eventLabel, eventValue, uaclient); }
/// <summary> /// Async track an event (non-blocking) /// </summary> /// <param name="httpContext">Current http context to extract payload from</param> /// <param name="eventCategory">Specifies the event category. Must not be empty</param> /// <param name="eventAction">Specifies the event action. Must not be empty</param> /// <param name="eventLabel">Specifies the event label</param> /// <param name="uaclient">Client override object</param> public void TrackEventAsync(HttpContextBase httpContext, string eventCategory, string eventAction, string eventLabel, UAClient uaclient = null) { // upfill and build TrackEventAsync(httpContext, eventCategory, eventAction, eventLabel, -1, uaclient); }
/// <summary> /// Track an event /// </summary> /// <param name="httpContext">Current http context to extract payload from</param> /// <param name="eventCategory">Specifies the event category. Must not be empty</param> /// <param name="eventAction">Specifies the event action. Must not be empty</param> /// <param name="uaclient">Client override object</param> public void TrackEvent(HttpContextBase httpContext, string eventCategory, string eventAction, UAClient uaclient = null) { // upfill and build TrackEvent(httpContext, eventCategory, eventAction, null, uaclient); }
/// <summary> /// Async track a page view (non-blocking) /// </summary> /// <param name="httpContext">Current http context to extract payload from</param> /// <param name="pageTitle">The title of the page / document</param> /// <param name="pageUrl">The path portion of the page URL. Should begin with '/'</param> /// <param name="hostName">Specifies the hostname from which content was hosted</param> /// <param name="uaclient">Client override object</param> public void TrackPageViewAsync(HttpContextBase httpContext, string pageTitle, string pageUrl, string hostName, UAClient uaclient = null) { // check for empty page url if (string.IsNullOrWhiteSpace(pageUrl)) throw new ArgumentNullException("pageUrl", "Google requires a valid URL to process page views."); // initialize the uaclient if null if (uaclient == null) uaclient = new UAClient(); // build the payload StringBuilder data = BuildPayload(BuildBasePayload(httpContext, uaclient), BuildPageTrackPayload(pageTitle, pageUrl, hostName)); dataQueue.EnqueueAsync(data); }
/// <summary> /// Async track a page view (non-blocking) /// </summary> /// <param name="httpContext">Current http context to extract payload from</param> /// <param name="pageTitle">The title of the page / document</param> /// <param name="pageUrl">The path portion of the page URL. Should begin with '/'</param> /// <param name="uaclient">Client override object</param> public void TrackPageViewAsync(HttpContextBase httpContext, string pageTitle, string pageUrl, UAClient uaclient = null) { // check for empty page url if (string.IsNullOrWhiteSpace(pageUrl)) throw new ArgumentNullException("pageUrl", "Google requires a valid URL to process page views."); // upfill and build TrackPageViewAsync(httpContext, pageTitle, pageUrl, (httpContext != null && httpContext.Request != null && httpContext.Request.Url != null) ? httpContext.Request.Url.Host : null, uaclient); }
/// <summary> /// Track a page view /// </summary> /// <param name="httpContext">Current http context to extract payload from</param> /// <param name="pageTitle">The title of the page / document</param> /// <param name="pageUrl">The path portion of the page URL. Should begin with '/'</param> /// <param name="uaclient">Client override object</param> public void TrackPageView(HttpContextBase httpContext, string pageTitle, string pageUrl, UAClient uaclient = null) { // check for empty page url if (string.IsNullOrWhiteSpace(pageUrl)) { throw new ArgumentNullException("pageUrl", "Google requires a valid URL to process page views."); } // upfill and build TrackPageView(httpContext, pageTitle, pageUrl, (httpContext != null && httpContext.Request != null && httpContext.Request.Url != null) ? httpContext.Request.Url.Host : null, uaclient); }
/// <summary> /// Async track a page view (non-blocking) /// </summary> /// <param name="pageTitle">The title of the page / document</param> /// <param name="pageUrl">The path portion of the page URL. Should begin with '/'</param> /// <param name="hostName">Specifies the hostname from which content was hosted</param> /// <param name="uaclient">Client override object</param> public void TrackPageViewAsync(string pageTitle, string pageUrl, string hostName, UAClient uaclient = null) { // check for empty page url if (string.IsNullOrWhiteSpace(pageUrl)) throw new ArgumentNullException("pageUrl", "Google requires a valid URL to process page views."); // upfill and build TrackPageViewAsync((IsHttpRequestAvailable) ? new HttpContextWrapper(HttpContext.Current) : null, pageTitle, pageUrl, hostName, uaclient); }
/// <summary> /// build the base payload for all requests regardless of type /// </summary> /// <param name="httpContext">the http context to extract data from</param> /// <param name="uaclient">the client to override data in payload</param> /// <returns></returns> private Dictionary <string, string> BuildBasePayload(HttpContextBase httpContext, UAClient uaclient) { // extract the request HttpRequestBase request = (httpContext != null) ? httpContext.Request : null; // dictionary to make it look cleaner below Dictionary <string, string> parameters = new Dictionary <string, string>(); // always required parameters parameters.Add("v", v); parameters.Add("tid", tid); parameters.Add("cid", uaclient.cid); // get anonymize value if (aip) { parameters.Add("aip", "1"); } // user agent // user agent override first if (!string.IsNullOrWhiteSpace(uaclient.ua)) { parameters.Add("ua", uaclient.ua); } // user agent extracted from the request else if (request != null && !string.IsNullOrWhiteSpace(request.UserAgent)) { parameters.Add("ua", request.UserAgent); } // ip address // ip address override first if (!string.IsNullOrWhiteSpace(uaclient.uip)) { parameters.Add("uip", uaclient.uip); } // ip extracted from the request, make sure this isnt a local request as well else if (request != null && !request.IsLocal && !string.IsNullOrWhiteSpace(request.UserHostAddress)) { parameters.Add("uip", request.UserHostAddress); } // document encoding, skip default de=UTF-8 if (request != null && request.ContentEncoding != null && !string.IsNullOrWhiteSpace(request.ContentEncoding.HeaderName) && request.ContentEncoding.HeaderName.ToUpper() != "UTF-8") { parameters.Add("de", request.ContentEncoding.HeaderName.ToUpper()); } // user language if (request != null && request.UserLanguages != null && request.UserLanguages.Length > 0) { parameters.Add("ul", string.Join(";", request.UserLanguages)); } // end return(parameters); }
/// <summary> /// build the base payload for all requests regardless of type /// </summary> /// <param name="httpContext">the http context to extract data from</param> /// <param name="uaclient">the client to override data in payload</param> /// <returns></returns> private Dictionary<string, string> BuildBasePayload(HttpContextBase httpContext, UAClient uaclient) { // extract the request HttpRequestBase request = (httpContext != null) ? httpContext.Request : null; // dictionary to make it look cleaner below Dictionary<string, string> parameters = new Dictionary<string, string>(); // always required parameters parameters.Add("v", v); parameters.Add("tid", tid); parameters.Add("cid", uaclient.cid); // get anonymize value if (aip) parameters.Add("aip", "1"); // user agent user agent override first if (!string.IsNullOrWhiteSpace(uaclient.ua)) parameters.Add("ua", uaclient.ua); // user agent extracted from the request else if (request != null && !string.IsNullOrWhiteSpace(request.UserAgent)) parameters.Add("ua", request.UserAgent); // ip address ip address override first if (!string.IsNullOrWhiteSpace(uaclient.uip)) parameters.Add("uip", uaclient.uip); // ip extracted from the request, make sure this isnt a local request as well else if (request != null && !request.IsLocal && !string.IsNullOrWhiteSpace(request.UserHostAddress)) parameters.Add("uip", request.UserHostAddress); // document encoding, skip default de=UTF-8 if (request != null && request.ContentEncoding != null && !string.IsNullOrWhiteSpace(request.ContentEncoding.HeaderName) && request.ContentEncoding.HeaderName.ToUpper() != "UTF-8") parameters.Add("de", request.ContentEncoding.HeaderName.ToUpper()); // user language if (request != null && request.UserLanguages != null && request.UserLanguages.Length > 0) parameters.Add("ul", string.Join(";", request.UserLanguages)); // end return parameters; }