Skip to content

Instantly share code, notes, and snippets.

@frueda1
Last active June 10, 2023 03:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frueda1/89e7e2a0d7f18ace95d9cccbd502ad61 to your computer and use it in GitHub Desktop.
Save frueda1/89e7e2a0d7f18ace95d9cccbd502ad61 to your computer and use it in GitHub Desktop.
Personalize class to call Sitecore Personalize API
public class Experience
{
[JsonProperty("clientKey", NullValueHandling = NullValueHandling.Ignore)]
public string ClientKey { get; set; }
[JsonProperty("channel", NullValueHandling = NullValueHandling.Ignore)]
public string Channel {get; set;}
[JsonProperty("language", NullValueHandling = NullValueHandling.Ignore)]
public string Language {get; set;}
[JsonProperty("currencyCode", NullValueHandling = NullValueHandling.Ignore)]
public string CurrencyCode {get; set;}
[JsonProperty("pointOfSale", NullValueHandling = NullValueHandling.Ignore)]
public string PointOfSale {get; set;}
[JsonProperty("browserId", NullValueHandling = NullValueHandling.Ignore)]
public string BrowserId {get; set;}
[JsonProperty("friendlyId", NullValueHandling = NullValueHandling.Ignore)]
public string FriendlyId { get; set; }
}
public class Personalize
{
public string GetDecisionModelResult(string experienceId)
{
try
{
SiteContext site = Sitecore.Context.Site;
HttpCookie cookie;
string bid = string.Empty;
if (site != null)
{
cookie = System.Web.HttpContext.Current.Request.Cookies["bid_" + Settings.GetSetting("ClientKey")];
if (cookie != null)
{
bid = cookie.Value;
var experience = new Experience
{
ClientKey = Settings.GetSetting("ClientKey"),
Channel = "WEB",
Language = "en",
CurrencyCode = "USD",
PointOfSale = Settings.GetSetting("PointOfSale"),
BrowserId = bid,
FriendlyId = experienceId
};
var json = JsonConvert.SerializeObject(experience);
var data = new StringContent(json, Encoding.UTF8, "application/json");
var url = Settings.GetSetting("ApiUrl");
var client = new HttpClient();
var response = client.PostAsync(url, data);
var result = response.Result.Content.ReadAsStringAsync();
dynamic JSONdata = JObject.Parse(result.Result);
var segment = JSONdata.visitorSegment.brand;
if (segment == "Unsegmented")
segment = string.Empty;
return segment;
}
}
}
catch (Exception ex) {
Log.Error("There was an issue on connecting the Personalize API " + ex.Message, this);
}
return string.Empty;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment