More in this section

Forums / Developing with Sitefinity / Google analytics code with library documents

Google analytics code with library documents

9 posts, 0 answered
  1. Mark
    Mark avatar
    2 posts
    Registered:
    21 Nov 2006
    17 Jul 2009
    Link to this post
    We have been adding PDF documents to the library module as the ability to include meta field data makes this a great feature.  However, to track document downloads with google analytics, additional code is needed with the hyperlink

    example:
    <a href="http://www.example.com/files/map.pdf" onClick="javascript: pageTracker._trackPageview('/downloads/map'); ">

    Looking for helop on how to accomplish this when using a control like the DownloadList. 
  2. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    22 Jul 2009
    Link to this post
    Hi Mark,

    Here is how you can change the DownloadList control and add attributes to the links inside it:

    1. Create a new class in the App_Code directory:
    CustomDownloadList.cs
    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Web; 
    using Telerik.News.WebControls; 
    using Telerik.News; 
    using System.Collections; 
    using Telerik.Cms.Engine; 
    using Telerik.Libraries; 
    using System.Web.UI.WebControls; 
    using Telerik.Framework.Web.Design; 
    using System.Web.UI; 
    using System.ComponentModel; 
     
    /// <summary> 
    /// Summary description for CustomDownloadList 
    /// </summary> 
    namespace Telerik.Samples 
        [ToolboxItem(typeof(CustomDownloadList))] 
        public class CustomDownloadList : DownloadList 
        { 
            public CustomDownloadList() 
            { 
            } 
     
            protected override void SetListMetadata(System.Web.UI.Control itemContainer, IContent contentItem) 
            { 
                base.SetListMetadata(itemContainer, contentItem); 
                HyperLink downloadLink = itemContainer.FindControl("DownloadLink"as HyperLink; 
                string script = @"javascript: pageTracker._trackPageview('/downloads/map');"
                downloadLink.Attributes.Add("onClick", script); 
            } 
            public override Type LocalizationAssemblyInfo 
            { 
                get 
                { 
                    return typeof(DownloadList); 
                } 
                set 
                { 
                    base.LocalizationAssemblyInfo = value; 
                } 
            } 
            public override System.Type AssemblyInfo 
            { 
                get 
                { 
                    return typeof(DownloadList); 
                } 
                set 
                { 
                    base.AssemblyInfo = value; 
                } 
            } 
     
        } 
     

    2. Register the new control in the web.config file, section <toolboxControls>:
     <add name="CDownloadList" section="New" type ="Telerik.Samples.CustomDownloadList, App_Code "/> 

    You can modify my code above as well, to achieve the exact functionality you want with the tracker.

    Don't hesitate to contact us if you have other questions.


    Greetings,
    Georgi
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. Chip Gallent
    Chip Gallent avatar
    41 posts
    Registered:
    06 Nov 2009
    08 Jun 2010
    Link to this post
    How would I change this line so that where it says '/downloads/map' - I could dynamically insert the file name in place of 'map' after

    string script = @"javascript: pageTracker._trackPageview('/downloads/map');";

    Thanks,
    Chip
  4. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    09 Jun 2010
    Link to this post
    Hello Chip Gallent,

    You can try something like the bellow where downloads and map will be some string parameters to which you will assign a value:
    string script = string.Format(@"javascript: pageTracker._trackPageview('/{0}/{1}');", downloads, map);

    Sincerely yours,
    Radoslav Georgiev
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  5. Chip Gallent
    Chip Gallent avatar
    41 posts
    Registered:
    06 Nov 2009
    09 Jun 2010
    Link to this post
    Sorry for being a noob, but I tried that and I'm having trouble grabbing the Title from the download list item. to insert as the second part of the string.format

    Here's what I added and I got a compilation error:
    string script = string.Format(@"javascript: pageTracker._trackPageview('/{0}/{1}');", "downloads", Literal.Title);

    Many Thanks,
    Chip
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    09 Jun 2010
    Link to this post
    Hello Chip Gallent,

    What is Literal.Title and what is the error you are getting? Is this Literal an existing object? You have to find the Hyperlink with ID="DownloadLink" as Rado's code shows. Then add  downloadLink.Text to string.Format method


    Kind regards,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  7. Chip Gallent
    Chip Gallent avatar
    41 posts
    Registered:
    06 Nov 2009
    09 Jun 2010
    Link to this post
    Here's what I added and this time got no errors:
    protected override void SetListMetadata(System.Web.UI.Control itemContainer, IContent contentItem)
            {
                base.SetListMetadata(itemContainer, contentItem);
                HyperLink downloadLink = itemContainer.FindControl("DownloadLink") as HyperLink;
                string script = string.Format(@"javascript: pageTracker._trackPageview('/{0}/{1}');", "downloads", downloadLink.Text);
                downloadLink.Attributes.Add("onClick", script);
            }

    Here is the literal I'm trying to pull the Title from - it's in this file -> /Sitefinity/ControlTemplates/Libraries/DownloadList.ascx
    <asp:HyperLink ID="DownloadLink" runat="server">
        <asp:Literal ID="Title" runat="server"></asp:Literal>
    </asp:HyperLink>

    Now when I uploaded the code at the top and got it running "downloadLink.Text" is not showing up in the live code here: http://www.nashvillechamber.com/Test.aspx
    I'm sure I'm not grabbing it correctly somehow. Should it be downloadLink.Title in the string.Format line above?

    Thanks again for the responses and help....as usual.

    - Chip
  8. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    10 Jun 2010
    Link to this post
    Hello Chip Gallent,

    Use contentItem.GetMetaData("Name").ToString() instead of downloadLink.Text

    All the best,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  9. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    10 Jun 2010
    Link to this post
    Hi Chip Gallent,

    Another option is getting the Literal control text and use it in the js function.

    ((System.Web.UI.WebControls.Literal)(downloadLink.Controls[1])).Text


    Sincerely yours,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Register for webinar
9 posts, 0 answered