Portal Engine Questions on portal engine and web parts.
Version 5.x > Portal Engine > Views Transformation View modes: 
User avatar
Member
Member
kelements - 3/29/2011 9:23:59 PM
   
Views Transformation
I have installed a Views Webpart on my blog posts. I would like to include a Views transformation in my Blog > PostPreview transformation so that users can see the views on the root of the blog.

Any suggestions?

User avatar
Kentico Developer
Kentico Developer
kentico_ivanat - 3/30/2011 8:53:55 AM
   
RE:Views Transformation
Hi,

you can create a custom transformation function and call from the transformation:

<%# Class1.GetPageViews(Eval("NodeID")) %>

Code of this custom function may look like:

public static string GetPageViews(Object nodeId)
{
int objectId = Convert.ToInt32(nodeId);

DateTime fromDate = CMS.GlobalHelper.DateTimeHelper.GetDayStart(DateTime.Now);
DateTime toDate = fromDate.AddDays(1);
CMS.WebAnalytics.HitsIntervalEnum interval = CMS.WebAnalytics.HitsIntervalEnum.Day;
int count = CMS.WebAnalytics.HitsInfoProvider.GetObjectHitCount(CMS.CMSHelper.CMSContext.CurrentSiteID, objectId, interval, "pageviews", fromDate, toDate);

return String.Format("{0}", CMS.WebAnalytics.HitsInfoProvider.GetObjectHitCount(CMS.CMSHelper.CMSContext.CurrentSiteID, objectId, interval, "pageviews", fromDate, toDate));
}


Using above code will be number of visits displayed for each blog.

Best regards,
Ivana Tomanickova

User avatar
Member
Member
kelements - 3/30/2011 12:01:32 PM
   
RE:Views Transformation
Hi Ivana,

I have followed you instructions and I am getting the following error:

Food [CMSDataProperties.LoadTransformation]: http://server/KenticoCMS/CMSTransformations/74f043ed-fcee-4626-af01-5439684f0837/cms/blog/postpreview.ascx(8): error CS0103: The name 'Class1' does not exist in the current context


Please help.

User avatar
Kentico Developer
Kentico Developer
kentico_ivanat - 3/31/2011 1:46:01 AM
   
RE:Views Transformation
Hi,

the code I provided you was just an example. Class1 is a name of class, in which was my custom transformation function inserted.

In case you have followed instructions from the above tutorial please replace Class1 with MyFunctions. MyFunctions is name of class in which custom transformation function is inserted in the documentation.

Best regards,
Ivana Tomanickova

User avatar
Member
Member
kelements - 3/31/2011 12:18:40 PM
   
RE:Views Transformation
Hi Ivana, I was able to get the new Class working and see that all post have ZERO views when in fact some have views. I believe it has to do with the start and end dates:

DateTime fromDate = CMS.GlobalHelper.DateTimeHelper.GetDayStart(DateTime.Now);
DateTime toDate = fromDate.AddDays(1);

I want to retrieve all post from all dates in the past, I tried the following code and it did not work:

DateTime fromDate = CMS.GlobalHelper.DateTimeHelper.GetDayStart(DateTime.Now);
DateTime toDate = fromDate.AddDays(-360);

Please advise.

User avatar
Kentico Developer
Kentico Developer
kentico_ivanat - 4/1/2011 2:02:01 PM
   
RE:Views Transformation
Hi,

following code will display total number of visits. I hope it helps you :)


public static string GetPageViews(Object nodeId)
{
int objectId = Convert.ToInt32(nodeId);

DateTime fromDate = new DateTime(1753, 1, 1);
DateTime toDate = new DateTime(9999, 1, 1);


CMS.WebAnalytics.HitsIntervalEnum interval = CMS.WebAnalytics.HitsIntervalEnum.Year;
int count = CMS.WebAnalytics.HitsInfoProvider.GetObjectHitCount(CMS.CMSHelper.CMSContext.CurrentSiteID, objectId, interval, "pageviews", fromDate, toDate);

return String.Format("{0}", CMS.WebAnalytics.HitsInfoProvider.GetObjectHitCount(CMS.CMSHelper.CMSContext.CurrentSiteID, objectId, interval, "pageviews", fromDate, toDate));
}


Additionaly, please make sure that the analytics module is enabled: Site Manager - Settings - Web Analytics - Enable web analytics

Best regards,
Ivana Tomanickova

User avatar
Member
Member
kelements - 4/1/2011 3:11:27 PM
   
RE:Views Transformation
I am not sure what this new code means but it work perfectly. Thanks!!!