Posted on 22/05/2018 12:05:19
Hi Jan
Yes, not the easiest to use - we are releasing an "Easy to use" API very soon.
Until then, please find code example below:
public string ConsentExample()
{
string currentVisitorId = Dynamicweb.Context.Current.Request.Cookies["Dynamicweb"]?.Values.Get("VisitorID");
ActivityService activityService = new ActivityService();
Activity activity = activityService.GetActivityById("Activity1");
ConsentService consentService = new ConsentService();
Consent consent = consentService.GetConsentById(activity.Id, currentVisitorId, "Visitor");
if ((consent.Status == ConsentStatus.Given))
{
// Visitor has given consent - track or whatever.
return "<script>trackingscript();</script>";
}
else
{
// Visitor has not given consent - display a "Give consent button" or record that the user has given us a consent
if (Dynamicweb.Core.Converter.ToBoolean(Context.Current.Request.GetString("GiveConsentForTracking")))
{
consentService.GiveConsent(activity.Id, currentVisitorId, "Visitor", ConsentRequestInfo.FromRequest(Context.Current.Request));
return "Thank you!";
}
else
{
return "<a href=\"Default.aspx?ID=123&GiveConsentForTracking=True\">Yes, please track me</a>";
}
}
}
public string RegisterConsent()
{
//Give consent
//?GiveEmailConsent=True&cemail=np@dynamicweb.com
//Withdraw consent
//?GiveEmailConsent=False&cemail=np@dynamicweb.com
//?GiveEmailConsent=True&RecipientId={{EmailMessaging:Recipient.Id}}&RecipientSecret={{EmailMessaging:Recipient.Secret}}
//Dynamicweb.Context.Current.Request.QueryString["cemail"];
if (!string.IsNullOrEmpty(Dynamicweb.Context.Current.Request.QueryString["GiveEmailConsent"]))
{
Int32 recipientId = Dynamicweb.Core.Converter.ToInt32(Dynamicweb.Context.Current.Request.QueryString[Dynamicweb.EmailMarketing.Constants.UnsubscribeRecipientIdRequestName]);
string recipientSecret = Dynamicweb.Context.Current.Request.QueryString[Dynamicweb.EmailMarketing.Constants.UnsubscribeRecipientSecretRequestName];
Dynamicweb.Mailing.Recipient recipient = Dynamicweb.Mailing.Recipient.GetRecipientById(recipientId);
if (recipient == null || recipient.IsNew || String.IsNullOrEmpty(recipientSecret) || !recipient.Secret.Equals(recipientSecret))
{
return "Secret does not match";
}
string emailAddress = recipient.EmailAddress;
Dynamicweb.DataProcessing.ActivityService activityService = new Dynamicweb.DataProcessing.ActivityService();
Dynamicweb.DataProcessing.Activity activity = activityService.GetActivityById("d723363c-2b56-4349-8abc-54bc8154d4d4");
Dynamicweb.DataProcessing.ConsentService consentService = new Dynamicweb.DataProcessing.ConsentService();
Dynamicweb.DataProcessing.Consent consent = consentService.GetConsentById(activity.Id, emailAddress, "Email");
if (consent != null && consent.Status == Dynamicweb.DataProcessing.ConsentStatus.Given)
{
if (Dynamicweb.Core.Converter.ToBoolean(Dynamicweb.Context.Current.Request.QueryString["GiveEmailConsent"]))
{
// Visitor has given consent.
return "Thanks, you already gave us your consent";
}
else
{
consentService.WithdrawConsent(activity.Id, emailAddress, "Email", Dynamicweb.DataProcessing.ConsentRequestInfo.FromRequest(Dynamicweb.Context.Current.Request));
return "Thank you - your consent has been withdrawn!";
}
}
else
{
// Visitor has not given consent - display a "Give consent button" or record that the user has given us a consent
if (Dynamicweb.Core.Converter.ToBoolean(Dynamicweb.Context.Current.Request.QueryString["GiveEmailConsent"]))
{
Dynamicweb.DataProcessing.Consent givenConsent = consentService.GiveConsent(activity.Id, emailAddress, "Email", Dynamicweb.DataProcessing.ConsentRequestInfo.FromRequest(Dynamicweb.Context.Current.Request));
return "Thank you - your consent has been registered! (" + givenConsent.SubjectId + ")";
}
}
}
return string.Empty;
}