The default behavior for Marketing Campaigns in Sitecore Analytics is such that they will only be applied to a visit if the campaign is applied on the first page of the visit. This could be a landing page flagged with that marketing campaign, or via the sc_camp
query string parameter.
I find this behavior to be somewhat problematic in certain commerce scenarios. It's also different than how Google Analytics handles marketing campaigns. Google Analytics will start a new visit for the user if he/she re-enters the site via a different marketing campaign.
I'd like to emulate this behavior in Sitecore Analytics for a POC I'm working on. I've attempted this via the initializeTracker
pipeline. I can successfully detect a change in the marketing campaign for the visit, but I'm unable to end and restart the visit. I've tried both utilizing Tracker.EndVisit()
and simply changing the ID of the visit. Neither seems to result in a new visit, associated with the marketing campaign.
Does anyone know how I can successfully end the previous visit, and start a new one, within the same request?
I am working 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();
}
}
}
Marketers can create campaign tags directly in the Sitecore Marketing Control Panel, or from the Campaign Creator tool.
If you're already using Google Analytics to track traffic to and on your website, you can link your Google Analytics account and Google Ads account. Once both accounts are linked, you can then see your Google Analytics metrics driven by ads in your Smart campaigns' overview.
As you wrote, by default, only the first campaign triggered in a visit gets assigned to it (inside Sitecore.Analytics.Pipelines.StartTracking.ProcessQueryString). If you need to update the campaign associated to a visit, you can hook into 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 to the current visit.
I've successfully made a few tests with this in SC 7.2, but you will want to test this more thoroughly as it is modifying default behaviour.
I have had a similar issue. A few solutions: 1. If actually you want a personalisation rule to trigger if a campaign has ever been triggered. Do as follows (This can be seen against support ticket I raised 407150):
Create new assembly with the condition class like 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("Could not convert 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 then you can look into 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 covered in more detail here:http://www.sitecore.net/Community/Technical-Blogs/Charlie-Darney/Posts/2014/06/Sitecore-Project-Create-Reset.aspx
To be honest Tracker.EndVisit(true); should do the trick, it invalidates the visit cookie, if you add true it invalidates the visitor, but you have to be careful where you call it, hence processor and button approach here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With