1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Adding code to all pages for tracking...

Adding code to all pages for tracking...

14 posts, 0 answered
  1. Armysniper
    Armysniper avatar
    126 posts
    Registered:
    03 Mar 2005
    24 Dec 2008
    Link to this post
    I want to be able to write code that is performed each time a page is loaded throughout the site. This code would be to track page access and the IP address of the user using the site. I have this code in my current web site but I want to incorporate it in Site Finity. Do I have to add this code to the Master page templates or is there a way to add it to the "aspx" page. Any examples of the code written if it is the Master page route would be great!
  2. SelAromDotNet
    SelAromDotNet avatar
    912 posts
    Registered:
    18 Jul 2012
    24 Dec 2008
    Link to this post
    adding to the master page should be simple enough unles it uses the code blocks < % and %> i think there is a problem with that.

    if that is the case, you can always wrap this code into a user control, and either place it in the master page, or add it to the template (which is pretty much a wrapper for the master page) in sitefinity.

    I have google tracking in my master page, I just pasted it before the /body tag and it works like a charm. hope this was helpful!
  3. Mike
    Mike avatar
    208 posts
    Registered:
    10 Dec 2007
    24 Dec 2008
    Link to this post
    I generally use the method selArom lists as his second option.  I always have a user control called WebAnalytics.ascx, and it's added to the toolbox controls section of the web.config so that it shows up in an Admin Controls category (where I also list other admin-only types of controls).   Then I add it to the template, rather than the page. 

    There are two reasons I do this.  One is that occasionally a site has pages that use a common template, but they don't want web tracking enabled for those pages.  The second is that some web analytics can take special variables that can be used to track related sets of pages.  Omniture's SiteCatalyst is one.  This allows me to set properties on the control depending on where in the site it's used, or however I want to organize it. 

    In that case, you simply create a second (or third, fourth, etc) template based on the same master page, add the user control and set the custom property.  Then any pages in that group will use that template, and get the web tracking set up correctly.

    But if it's just Google analytics, adding it to the master page body works too.  ;^)

    Regards,
    Mike Sharp
  4. Armysniper
    Armysniper avatar
    126 posts
    Registered:
    03 Mar 2005
    27 Dec 2008
    Link to this post
    Its actually my own internal code. In my current site I use a base page class that does all the work and can be turned off at the page level or at the site level through web.config. It allows me to track page access, button presses, hyperlink jumps, whatever goes on that page. We use an analytics package (Omniture) but that is more for the Marketing team to use. My code is for my mode detailed tracking. I also use it for our subsites which are not always tracked using Omniture. I like the idea of placing it in a control. Guess I have to rethink how I approach this site now that we are looking to move it to SiteFinity.

    Thanks for the ideas! ;)
  5. Mike
    Mike avatar
    208 posts
    Registered:
    10 Dec 2007
    27 Dec 2008
    Link to this post

    Oh, if you already have a base page class you use, can it inherit from the Sitefinity InternalPage class?  I do something similar where I like to modify certain things on every page (such as the browser title bar).  So I have a base page class, called SmartTitleInternalPage that inherits from the sitefinity InternalPage.  Georgi showed me this trick, and it's pretty handy, assuming you don't have a different inheritance hierarchy that precludes you from using the Sitefinity InternalPage class.

    The control is nice because it works on any site, and allows you to expose properties in a configuration editor so that end users can set configuration values, but if you already have the code in a base class it seems easier to leave it that way.  You just need to be sure to call the Sitefinity base class methods, like

    public class SmartTitleInternalPage : InternalPage  
    {  
        protected override void OnPreInit(EventArgs e)  
        {  
            base.OnPreInit(e);  
            if (!Telerik.CmsContext.IsCrawlerRequest)  
            {  
                this.Title = GetFullPath(SiteMap.CurrentNode);  
            }  
        }  


    Regards,
    Mike Sharp
  6. Armysniper
    Armysniper avatar
    126 posts
    Registered:
    03 Mar 2005
    27 Dec 2008
    Link to this post
    Thanks for the info I will definitely take a look at your code. My only question is, how do I get this page class to be assigned to all pages on a site? In my current site, we dont use a CMS so each page is created and they inherit from my base page class automatically (created a Visual Studio item type for my pages). Is this a property that is set in the CMS side or is it something I can get to in the project level. I want to avoid CMS authors from having to do anything other than write content. ;)
  7. Mike
    Mike avatar
    208 posts
    Registered:
    10 Dec 2007
    27 Dec 2008
    Link to this post

    Sorry, I guess I left that part out! 

    In Sitefinity, the page processing pipeline starts with CmsEntryPoint.aspx, which is located in the Sitefinity folder.   So if you set that page to inherit from your new base page, all CMS pages will inherit the new classes.  So, put your new Base page in App_Code, and change/insert a tag something like this in CmsEntryPoint.aspx  (this is the only thing in that page for me):

    <%@ Page Inherits="SmartTitleInternalPage" MasterPageFile="~/Sitefinity/Dummy.master" %>

    In my case the SmartTitleInternalPage class becomes a code-behind for all pages.  Of course, that page inherits from InternalPage, so all Sitefinity functionality is still there.  I just get the opportunity to do a global override and insert custom code.

    Regards,
    Mike Sharp

  8. fregas baratis
    fregas baratis avatar
    185 posts
    Registered:
    14 Nov 2002
    27 Dec 2008
    Link to this post
    Wouldn't this type of tracking be better suited to going to the global.asax?  Then it will work no matter what master or controls are used on any page.

    Just a thought.

    craig
  9. Mike
    Mike avatar
    208 posts
    Registered:
    10 Dec 2007
    27 Dec 2008
    Link to this post

    Possibly, but since he already has a page base class, it seems better to use the special Sitefinity entry page that allows you to insert your class into the inheritance hierarchy. I usually use the global.asax for application and session scope code.  For example, in my current project I have some Commercefinity code that migrates the shopping cart from an anonymous session to a logged-in session when a user logs in. 

    Even so, I would still use a user control for webtracking for Omniture.  Site Catalyst is an awesome tool for analytics (albeit expensive), but in order to leverage you need to be able to set variables based on instances of a page.  For example, his marketing team might want to track a set of pages as a group (part of a campaign), so they would add the control to an instance of a page template which is used to create those pages.  Then they have an admin interface where they can specify a campaign name, which is set in the Omniture javascript code.  They would have a second template based on the same master page that had no tracking code whatsoever, so that they can tag individual pages separately, or exclude them from tracking.

    I suspect that in the end, armysniper is going to use all three approaches to some extent.

    Regards,
    Mike Sharp

  10. Armysniper
    Armysniper avatar
    126 posts
    Registered:
    03 Mar 2005
    27 Dec 2008
    Link to this post
    Mike, do you have an example of code for working with Omniture? We use a different analytics tool for our current site but recently purchased Omniture. Would be cool to see how you would implement that control.
  11. Armysniper
    Armysniper avatar
    126 posts
    Registered:
    03 Mar 2005
    27 Dec 2008
    Link to this post
    We are looking to product our site as it has a cool job board that other non-profits and for profits have been asking to use. So I want the ability to package up our site as templates (one of the big draws to Site Finity over Ektron or other ASP.NET heavy CMS systems). Having it in the Global.asax would not be a bad idea but I want to be able to make it so that our Marketing team can create a new site from our site without touching code if it is possible.
  12. Mike
    Mike avatar
    208 posts
    Registered:
    10 Dec 2007
    28 Dec 2008
    Link to this post

    Another big draw to Sitefinity over Ektron, especially for non-profits, is price.  I tried like crazy to get Ektron to license their CMS in a package, but they wouldn't budge from the 10-seat minimum.  This priced it out of the range of most of my smaller clients.  That's actually when I first started using Sitefinity.  I do have one client that uses Ektron (it is a very nice CMS), and I hear they've restructured their licensing now, but I really like Sitefinity, now that I've started to get used to it.  There are still a few areas I don't quite understand, but I'm working on it.

     

    Anyway, I do have an example of an Omniture SiteCatalyst control, but wrote it for MCMS 2002 when I was working at the Gates Foundation.  I've attached a copy, but it's pretty much done the way Omniture's implementation guide shows it.  I think the whole implementation took me less than a day to do.  I spent more time reading their manuals.  ;^) 

    This was several years ago, and it's very likely some of this has changed, but it should give you some ideas.  There's another control embedded in there (meta.ascx) that sets up the site metadata that I use to tag the SiteCatalyst variables.  Nothing too exotic, but it's really specific to MCMS.  I added a ViewMode property to the meta.ascx so that it could be used with the web tracking control (it was normally responsible for writing meta tags in the page head, and provided an authoring interface).  Since it already had properties for all the metadata I wanted, it saved me a bunch of time.  Sitefinity has a much different mechanism for metadata.

     

    1 <%@ Control Language="c#" AutoEventWireup="false" Codebehind="WebTracking.ascx.cs" Inherits="Templates.Controls.WebTracking" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%> 
    2 <%@ Register TagPrefix="uc1" TagName="Meta" Src="BColumn/Meta.ascx" %> 
    3  
    4 <uc1:Meta id="WebTrack" viewmode="webtracking" runat="server" /> 
    5 <%  
    6 if (WebTrack.IsWebTrackingEnabled)  
    7 {  
    8 %> 
    9 <!-- SiteCatalyst code version: G.6.  
    10 Copyright 1997-2004 Omniture, Inc. More info available at  
    11 http://www.omniture.com --><script language="JavaScript"><!--  
    12 /* You may give each page an identifying name, server, and channel on  
    13 the next lines. */  
    14 var s_pageName="<%= WebTrack.PageName %>" 
    15 var s_server="<%= WebTrack.ServerName %>" 
    16 var s_channel="<%= WebTrack.ChannelPath %>" 
    17 var s_pageType="" 
    18 var s_prop1="<%= WebTrack.MediaType %>" 
    19 var s_prop2="<%= WebTrack.PrintVersion %>" 
    20 var s_prop3="<%= WebTrack.Prop3 %>" 
    21 var s_prop4="" 
    22 var s_prop5="" 
    23 /* E-commerce Variables */  
    24 var s_campaign="" 
    25 var s_state="" 
    26 var s_zip="" 
    27 var s_events="" 
    28 var s_products="" 
    29 var s_purchaseID="" 
    30 var s_eVar1="" 
    31 var s_eVar2="" 
    32 var s_eVar3="" 
    33 var s_eVar4="" 
    34 var s_eVar5="" 
    35 /********* INSERT THE DOMAIN AND PATH TO YOUR CODE BELOW ************/  
    36 /********** DO NOT ALTER ANYTHING ELSE BELOW THIS LINE! *************/  
    37  
    38 var s_code=' '//--></script>  
    39  
    40 <script language="JavaScript" src="<%= WebTrack.ScriptBaseUrl %>s_code.js"></script> 
    41 <script language="JavaScript"><!--  
    42 var s_wd=window,s_tm=new Date;if(s_code!=' '){s_code=s_dc(  
    43 '<%= WebTrack.ReportSuiteId %>');if(s_code)document.write(s_code)}else  
    44 document.write('<im'+'g src="http://<%= WebTrack.ReportSuiteId %>.112.2O7.net/b/ss/<%= WebTrack.ReportSuiteId %>/1/G.6--FB/s'+s_tm.getTime()+'?[AQB]'  
    45 +'&j=1.0&[AQE]" height="1" width="1" border="0" alt="" />')  
    46 //--></script><script language="JavaScript"><!--  
    47 if(navigator.appVersion.indexOf('MSIE')>=0)document.write(unescape('%3C')+'\!-'+'-')  
    48 //--></script><noscript><img 
    49 src="http://<%= WebTrack.ReportSuiteId %>.112.2O7.net/b/ss/<%= WebTrack.ReportSuiteId %>/1/G.6--NS/0" 
    50 height="1" width="1" border="0" alt="" /></noscript><!--/DO NOT REMOVE/--> 
    51 <!-- End SiteCatalyst code version: G.6. --> 
    52 <%  
    53 }  
    54 %> 
    55  
    56  
    57  
    58  

    Actually, there's way too much use of the Response.Write shortcut, and one really ugly conditional.  If I was writing things from scratch, I would do it differently.  I've become rather enamored with using resource files and embedding variable data using the String.Format() method.  Nice clean code, and yet it doesn't embed a bunch of formatting in the compiled C#.  So now instead of using this in the ascx file:

    var s_pageName="<%= WebTrack.PageName %>"

    I'd use something more like this in the ascx.cs file:

    litPageName.Text = String.Format(Messages.s_pageName, WebTrack.PageName);

    where Messages.s_pageName was in a resx file, and looked like:

    var s_pageName="{0}"

    But truthfully, I don't know if that's *really* better.  It's just a different way of doing the same thing, except that you can alter the value of the literal at runtime from another control, and you can move that ugly  if (WebTrack.IsWebTrackingEnabled)  conditional block into the codebehind.

    But if you wanted to programmatically touch the web tracking control from other areas of the page, you'd probably implement a WebTracking class, and implement the part that writes the javascript to the page in a ToString() or Render() method. 

    The idea of a web tracking control for SiteCatalyst is a good one.  I was thinking of writing one myself, but the client I had in mind decided to go with Google analytics (price).  They had been using Manticore, but that one is also very expensive, and I don't think they were really making use of it's features. 

    The group I worked with at the Foundation really liked Omniture SiteCatalyst, though.  I'm very impressed by it's capabilities. 

    Actually, a control like that should probably be part of the Sitefinity core...Just about everyone wants to use some sort of analytics these days. 


    One last bit about your jobs board.  You should probably figure out how Modules work, and write it using that architecture.  That way it's directly pluggable into any Sitefinity installation, and is much less likely to break on upgrades. 

    Ok, back to work for me!  ;^)

    Regards,
    Mike Sharp

     

  13. Armysniper
    Armysniper avatar
    126 posts
    Registered:
    03 Mar 2005
    29 Dec 2008
    Link to this post
    Funny you should mention the Bill and Melinda Gates Foundation. They are one of our biggest customers. :) We already use Ektron for one of our sites because our company got entranced with the idea of using it. In reality I introduced them to it never realizing how HORRIBLE it is as an API. Its a nice CMS in some ways if you want to get it to work as just a basic site, but if you want to customize it good luck. One case as an example, SiteFinity uses their own Membership provider but it still allows you to access the membership through the API built into the Security API of ASP.NET. Ektron BROKE this to get theirs to work and this is how they do alot of things.

    I used to work on the ASP.NET team so I am extremely experienced in ASP.NET functionality and features...in fact for this company's first iteration, I used the Community Starter Kit because it was an example of many of the features that ASP.NET 2.0 would include such as virtualized pages. My only questions come from wanting to navigate through Site Finity to get it up and running since I have a limited amount of time to demonstrate to my company why we DONT want to use Ektron. From a person who worked on the .NET 1.0 team...its a nightmare to use that product.

    BTW: So you guys dont think I am talking out my rump...check out my book on Amazon on J++ 6.0. WFC was the precursor to .NET. :)
  14. Armysniper
    Armysniper avatar
    126 posts
    Registered:
    03 Mar 2005
    29 Dec 2008
    Link to this post
    I will take a look at the modules and see if they fit for the job board. My job board has been a series of Web User controls for years and we are rebuilding them to meet new needs. They are VERY self sufficient controls and do not rely on anything other than maybe touching Site Finity's content for help text. I want to allow my marketing team the ability to add new help text to it as they see fit (they love to add fluff to the control to help out our nonprofit clients who might not be very computer literate). I don't give them much control as it is a very self contained unit and is mostly controlled via web.config. That is always the safest bet since there any changes can be made on ANY ASP.NET platform.
Register for webinar
14 posts, 0 answered