Creating Campaign Marketing in Sitecore Analytics Acts Like Google Analytics

The default behavior for marketing campaigns in Sitecore Analytics is that they will only apply to the visit if the campaign is applied on the first page of the visit. This can be a landing page tagged with this marketing campaign, or using the sc_camp query string parameter.

I believe that this behavior is somewhat problematic in some trading scenarios. This also differs from how Google Analytics handles marketing campaigns. Google Analytics will begin a new visit for a user if they re-enter the site using a different marketing campaign .

I would like to emulate this behavior in Sitecore Analytics for the POC I'm working on. I tried to execute this through the initializeTracker pipeline. I can successfully detect changes in the marketing campaign for the visit, but I cannot finish and restart the visit. I tried using Tracker.EndVisit() and just changing the visit id. This does not seem to lead to a new visit related to the marketing campaign.

Does anyone know how I can successfully complete a previous visit and start a new one, within the same request?

I work in CMS / DMS 7.1 rev 140130. My current code is below.

 using System; using System.Web; using Sitecore.Analytics; using Sitecore.Analytics.Pipelines.InitializeTracker; using Sitecore.Analytics.Web; using Sitecore.Configuration; using Sitecore.Data; using Sitecore.Web; namespace ActiveCommerce.Training.PriceTesting.Analytics { public class RestartVisitOnNewCampaign : InitializeTrackerProcessor { public override void Process(InitializeTrackerArgs args) { if (HttpContext.Current == null) { args.AbortPipeline(); } //no need to restart visit if visit is new if (Tracker.CurrentVisit.VisitPageCount < 1) { return; } //look for campaign id in query string Guid campaign; var campaignStr = WebUtil.GetQueryString(Settings.GetSetting("Analytics.CampaignQueryStringKey")).Trim(); if (string.IsNullOrEmpty(campaignStr) || !Guid.TryParse(campaignStr, out campaign)) { return; } //don't restart if the campaign isn't changing if (!Tracker.CurrentVisit.IsCampaignIdNull() && Tracker.CurrentVisit.CampaignId == campaign) { return; } //Tracker.EndVisit(false); //restart visit by setting new ID var visitCookie = new VisitCookie(); visitCookie.VisitId = ID.NewID.Guid; visitCookie.Save(); } } } 
+6
source share
6 answers

Thank you all for your input. Andrew's answer was actually the closest, so I rewarded him with generosity.

In the end, the problem turned out to be a property of Visitor.Settings.IsFirstRequest , which was ultimately obtained from VisitCookie.IsFirstRequest . If this property is false, a new visit will not be created, and the current page request will not be associated with a new visit. This must also happen in order for the page to be classified as a β€œlanding page” that would associate the campaign with it.

IsFirstRequest set to VisitCookie.Load() and compares the ASP.NET session identifier in the cookie with the current ASP.NET session identifier. This is why it was not possible to delete the cookie or change the visit ID. Unfortunately, there is no way to change the value of the session ID using the VisitCookie object, so the easiest task that seems to work is to simply delete the cookie value just before starting the Sitecore.Analytics.Pipelines.InitializeTracker.Initialize processor.

From my testing, this leads to a new visit when the campaign ID changes. I see this both in the Visits table and when personalizing based on the campaign ID.

 namespace ActiveCommerce.Training.PriceTesting.Analytics { public class RestartVisitOnNewCampaign : InitializeTrackerProcessor { public override void Process(InitializeTrackerArgs args) { if (HttpContext.Current == null) { args.AbortPipeline(); return; } //no need to restart visit if visit is new if (Tracker.Visitor.Settings.IsNew || Tracker.Visitor.Settings.IsFirstRequest || Tracker.CurrentVisit.VisitPageCount < 1) { return; } //look for campaign id in query string Guid campaign; var campaignStr = WebUtil.GetQueryString(Settings.GetSetting("Analytics.CampaignQueryStringKey")).Trim(); if (string.IsNullOrEmpty(campaignStr) || !Guid.TryParse(campaignStr, out campaign)) { return; } //don't restart if the campaign isn't changing if (!Tracker.CurrentVisit.IsCampaignIdNull() && Tracker.CurrentVisit.CampaignId == campaign) { return; } var current = HttpContext.Current; var cookie = current.Response.Cookies["SC_ANALYTICS_SESSION_COOKIE"]; if (cookie == null) { cookie = new HttpCookie("SC_ANALYTICS_SESSION_COOKIE"); current.Response.Cookies.Add(cookie); } cookie.Value = ""; } } } 
0
source

I had a similar problem. A few solutions: 1. If you really want the personalization rule to run if the campaign has ever triggered. Follow these steps (this is visible on the support ticket, I raised 407150):

  • Go to "/ sitecore / system / Settings / Rules / Definitions / Elements / Visit / Campaign has been started" and duplicate this element
  • In the copied element, change the text, for example. "where the campaign [CampaignId, Tree, root = / sitecore / system / Marketing Center / Campaigns, specific] ever triggered during the current visit"
  • In the copied element, change the type to your own class and your own assembly
  • Create a new assembly with a condition class, as in the following example:

    public class HasCampaignAtAllCondition: WhenCondition, where T: RuleContext {private Guid CampaignGuid {get; set; } public string CampaignId {get; set; } protected override bool Execute (T ruleContext) {Assert.ArgumentNotNull (ruleContext, "ruleContext"); try {this.CampaignGuid = new Guid (this.CampaignId); } catch {Log.Warn (string.Format ("Failed to convert the value to guid: {0}", this.CampaignId), base.GetType ()); return false; } return Tracker.Visitor.DataSet.PageEvents.Any (row => row.PageEventDefinitionId == new Guid ("{F358D040-256F-4FC6-B2A1-739ACA2B2983}") && row.Data == this.CampaignId); }
    }

If you want to reset a visit, you can see the following:

 if (HttpContext.Current.Request.Cookies["SC_ANALYTICS_GLOBAL_COOKIE"] != null) { if (!string.IsNullOrEmpty(HttpContext.Current.Request.Cookies["SC_ANALYTICS_GLOBAL_COOKIE"].Value) && QueryStringHelperFunctions.GetQueryStringBool(HttpContext.Current.Request.QueryString, "forcenewvisitor", false)) { HttpContext.Current.Response.Cookies["SC_ANALYTICS_GLOBAL_COOKIE"].Value = ""; HttpContext.Current.Response.Cookies["SC_ANALYTICS_SESSION_COOKIE"].Value = ""; } } 

This is described in more detail here: http://www.sitecore.net/Community/Technical-Blogs/Charlie-Darney/Posts/2014/06/Sitecore-Project-Create-Reset.aspx

Honestly, Tracker.EndVisit (true); should do the trick, it will invalidate the visit cookie, if you add true, this invalidates the visitor, but you have to be careful where you call it, therefore, the processor and button are used here.

+1
source

As you wrote, by default, only the first campaign that is triggered during the visit is assigned to it (inside Sitecore.Analytics.Pipelines.StartTracking.ProcessQueryString). If you need to update the campaign associated with the visit, you can connect to the triggerCampaign pipeline and set the value manually.

 public class AlwaysSetCampaignId : TriggerCampaignProcessor { public override void Process(TriggerCampaignArgs args) { // Set the campaign ID to the current visit Sitecore.Analytics.Tracker.CurrentVisit.CampaignId = args.Definition.ID.ToGuid(); } } 

This will not create a new visit, but it will change the CampaignId associated with the current visit.

I have successfully completed several tests with this in SC 7.2, but you will want to test this more carefully since it changes the default behavior.

+1
source

This has not been fully tested, but it looks like a new entry is being created in the Visites table with the corresponding CampaignId when I change the querystring sc_camp . Basically, I pulled out the CreateVisit method from the Sitecore.Analytics.Pipelines.InitializeTracker.Initialize pipeline processor (along with several other private support methods - argh, private methods!).

Not very elegant, but it seems to work, although several conditional checks may be required to create a new visit. I placed this processor after Sitecore.Analytics.Pipelines.InitializeTracker.Initialize processor .

 public class RestartVisitOnNewCampaign : InitializeTrackerProcessor { public override void Process(InitializeTrackerArgs args) { if (HttpContext.Current == null) { args.AbortPipeline(); } //no need to restart visit if visit is new if (Tracker.CurrentVisit.VisitPageCount < 1) { return; } //look for campaign id in query string Guid campaign; var campaignStr = WebUtil.GetQueryString(Settings.GetSetting("Analytics.CampaignQueryStringKey")).Trim(); if (string.IsNullOrEmpty(campaignStr) || !Guid.TryParse(campaignStr, out campaign)) { return; } //don't restart if the campaign isn't changing if (!Tracker.CurrentVisit.IsCampaignIdNull() && Tracker.CurrentVisit.CampaignId == campaign) { return; } Tracker.EndVisit(false); //restart visit by setting new ID //var visitCookie = new VisitCookie(); //visitCookie.VisitId = ID.NewID.Guid; //visitCookie.Save(); Visitor visitor = Tracker.Visitor; CreateNewVisit(HttpContext.Current, visitor); } protected virtual void CreateNewVisit(HttpContext httpContext, Visitor visitor) { VisitorDataSet.VisitsRow currentVisit = visitor.CurrentVisit; if ((currentVisit == null) || (currentVisit.VisitId != visitor.CookieVisitId)) { currentVisit = visitor.CreateVisit(visitor.CookieVisitId); } currentVisit.AspNetSessionId = WebUtil.GetSessionID(); HttpRequest request = httpContext.Request; byte[] ip = GetIp(request.UserHostAddress ?? string.Empty); string majorName = request.Browser.Browser; string minorName = request.Browser.Version; string version = request.Browser.Version; string str4 = request.Browser.Platform; string str5 = string.Empty; string str6 = string.Empty; currentVisit.Ip = ip; currentVisit.Browser = visitor.DataContext.GetBrowser(majorName, minorName, version); currentVisit.UserAgent = visitor.DataContext.GetUserAgent(request.UserAgent ?? string.Empty); currentVisit.GeoIp = visitor.DataContext.GetGeoIp(ip); currentVisit.Location = visitor.DataContext.GetLocation(string.Empty, string.Empty); currentVisit.RDNS = request.UserHostName ?? string.Empty; currentVisit.OperatingSystem = visitor.DataContext.GetOperatingSystem(str4, str5, str6); currentVisit.Screen = visitor.DataContext.GetScreen(GetDimensions(request)); currentVisit.DeviceName = (Sitecore.Context.Device == null) ? string.Empty : Sitecore.Context.Device.Name; currentVisit.Language = Sitecore.Context.Language.Name; SiteContext site = Sitecore.Context.Site; if (site != null) { currentVisit.MultiSite = site.Name; } var args = new CreateVisitArgs(currentVisit, request); CreateVisitPipeline.Run(args); } protected virtual byte[] GetIp(string userHostAddress) { IPAddress address; if (IPAddress.TryParse(userHostAddress, out address)) { return address.GetAddressBytes(); } Log.Warn("Failed to parse ip address: " + userHostAddress, this); return new byte[4]; } protected virtual string GetDimensions(HttpRequest request) { HttpBrowserCapabilities browser = request.Browser; if (browser == null) { return string.Empty; } return string.Format("{0}x{1}", browser.ScreenPixelsWidth, browser.ScreenPixelsHeight); } } 
+1
source

Have you tried calling Invalidate () in a cookie as well as InvalidateVisitorCache ()?

0
source

Source: https://habr.com/ru/post/971662/


All Articles