Kentico MVP Brian McKeiver (@mcbeev) covered this subject in his post a while back. I'd like to suppliment his posts with what I learned when following his instructions to implement Disqus on my Kentico site. The differences or changes aren't substantial but they are worth mentioning. So I'm unofficially naming this post "Part 2 1/4" of How to Connect Disqus Comments to Kentico EMS Activity Tracking.
Adding Disqus to your Kentico site is pretty slick and easy as you can see in
Brian's initial post. Brian continues
in another post on how to setup logging the Disqus comment activity to Kentico. I read and re-read both of Brian's posts and
learned quite a bit. This was my first time dealing with Web API 2 and things are actually pretty slick, I think I'll be doing more of that soon.
Note, In Brian's post he was working in 8.1, as of this post date, I'm working in 8.2.
Logging Unique Properties to Disqus
I'm not 100% sure but the API for Disqus may have changed since Brian wrote his initial post. One thing I notice, after I took the 5 minutes to setup my website, and add the Disqus code to my site, is I started seeing weird links showing up in Disqus.
In the Disqus code provided they talk about using unique identifiers and canonical URLs to prevent duplicate threads. So I
read the documentation and low and behold they were right. This is what I was experiencing.
My fix, add a few macros in the Disqus config javascript:
var disqus_config = function () {
this.page.url = '{ % CurrentDocument.AbsoluteURL % }';
this.page.identifier = '{ % CurrentDocument.NodeGuid % }';
this.callbacks.onNewComment = [function (comment) {
//Let Kentico know we had comment activity
$.post("{ % RootDocument.AbsoluteURL % }kdapi/logdisquscomment", { '': comment.id });
}];
};
Notice callbacks is using the RootDocument.AbsoluteURL macro, this value is what is set in the Site>your website url. So if you have it set to localhost it will return that in your production environment.
After I added the macros, I didn't experience any other duplicate threads.
I'm not using MVC
For my site, I have it setup as a standard website so I needed to convert the MVC code Brian wrote. What I did was created two class files in my App_Code directory.
LogDisqusComment.cs
using CMS.Helpers;
using CMS.Localization;
using CMS.OnlineMarketing;
using CMS.SiteProvider;
using CMS.WebAnalytics;
using System.Net;
using System.Web.Http;
/// <summary>
/// Handles API Methods for Disqus client side action tracking
/// </summary>
public class LogDisqusCommentController : ApiController
{
/// <summary>
/// Log new Activity and Conversion for Disqus Comment
/// </summary>
/// <param name="CommentID">CommentID from Disqus API</param>
/// <returns>string OK on success</returns>
public string Post([FromBody] string CommentID)
{
var currentContact = OnlineMarketingContext.CurrentContact;
// Do not proceed, if OnlineMarketing is disabled
if (currentContact == null)
{
throw new HttpResponseException(HttpStatusCode.Forbidden);
}
//Create an Activity record
ActivityInfo newActivity = new ActivityInfo()
{
ActivityType = "DisqusComment",
ActivityTitle = string.Format("Comment Added to Document {0}", string.Empty),
ActivitySiteID = SiteContext.CurrentSiteID,
ActivityValue = string.Format("Disqus CommentID: {0}", CommentID),
ActivityOriginalContactID = currentContact.ContactID,
ActivityActiveContactID = currentContact.ContactID,
ActivityIPAddress = RequestContext.UserHostAddress, // notice adding IP tracking as it was missing from Brian's post
ActivityURL = CMS.Helpers.RequestContext.URLReferrer, //switching this around on purpose so UI shows which page we received the comment on
ActivityURLReferrer = CMS.Helpers.RequestContext.URL.AbsoluteUri, //switching this around on purpose so UI shows referrer as the API call
};
ActivityInfoProvider.SetActivityInfo(newActivity);
//Treat it as a Conversion
string siteName = SiteContext.CurrentSiteName;
if (AnalyticsHelper.TrackConversionsEnabled(siteName))
{
HitLogProvider.LogConversions(siteName, LocalizationContext.PreferredCultureCode, "DisqusComment", 0, 1, 1);
}
return "OK";
}
}
LogDisqusCommentModule.cs (yes the name could have been different but didn't want to change it)
using CMS.Base;
using System.Web.Http;
[LogDisqusCommentModuleLoader]
public partial class CMSModuleLoader
{
private class LogDisqusCommentModuleLoaderAttribute : CMSLoaderAttribute
{
/// <summary>
/// Initializes the module. Called when the application starts.
/// </summary>
public override void Init()
{
base.Init();
GlobalConfiguration.Configuration.Routes.MapHttpRoute("kdapi", "kdapi/{controller}/{id}", new { id = RouteParameter.Optional });
}
}
}
Yes the class name and module could have been named different since it really has nothing to do with the Disqus comments but at any rate you get the idea. Now I'm not 100% sure this initialization module was needed because I believe Kentico has this already configured OOTB but
according to the documentation, it was, so I included and I haven't tried to break it by removing it, yet!
no IP Address for the activity
This isn't a huge issue but the fix is very easy. In my code above you can see I've already added it in so copy and paste away.
So that's it for me. Thanks again Brian for all the leg work on your initial posts and providing me some great insturctions to get the job done right!
Best of luck and Happy Coding!