Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Mov3nforward 117 posts 319 karma points
    Feb 11, 2021 @ 16:46
    Mov3nforward
    0

    Track downloads for links directly to PDF file.

    Is there are package that will track downloads of a pdf? If a user has a link to a pdf directly and downloads the pdf without going to a webpage, how would you track that event? WordPress has a plugin to do this and I have a client asking to do the same in Umbraco.

    https://carlhendy.com/pdf-analytics/

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Feb 11, 2021 @ 16:56
    Nik
    0

    You will probably want to create a HttpHandler (I think that's the right thing) that can hook in nice and early into your request pipeline at an IIS level. Then you should be able to get the URL and identify if it is a PDF of not, then post a logging/tracking entry where ever you need it to be stored in order to report on it.

    This is all theoretical though :-D

    Nik

  • Brendan Rice 538 posts 1099 karma points
    Feb 11, 2021 @ 17:47
    Brendan Rice
    0

    It seems like you are looking for something like this:

    https://www.datadrivenu.com/track-downloads-google-analytics/

  • Mov3nforward 117 posts 319 karma points
    Feb 17, 2021 @ 20:43
    Mov3nforward
    0

    GA alone will not work in this instance. If a user has the direct link to a pdf like https://abc.com/filename.pdf, GA will not be able to track this. There is a WordPress plugin that does this and sends the tracking information to GA, but I can't seem to find anything like that for Umbraco. Umbraco needs to stop so much development on the CMS (every single release has bugs) and begin to provide robust packages that rival WordPress. Just to setup a shopping cart in Umbraco is a PIA while my clients are setting up carts and their own GTM tagging with WordPress.

  • Brendan Rice 538 posts 1099 karma points
    Feb 17, 2021 @ 20:52
    Brendan Rice
    0

    Niks suggestion will work.

    The power of Umbraco is that it is developer friendly. It allows developers to extend it easily.

    One of the biggest benefits of WordPress is the amount of plugins. It's also it's biggest weakness.

  • Mov3nforward 117 posts 319 karma points
    Feb 17, 2021 @ 20:57
    Mov3nforward
    0

    I understand the weaknesses of WordPress, but when a client is coming from WordPress and we have to build something that WordPress already does, Umbraco doesn't look so good. No one who is making money in this business has time to create every feature from scratch.

  • Brendan Rice 538 posts 1099 karma points
    Feb 17, 2021 @ 23:01
    Brendan Rice
    1

    Here's how to do it:

    Step 1

    Copy the class below into your code:

    using System;
    using System.Web;
    
    public class HelloWorldModule : IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.LogRequest += new EventHandler(LogRequest);
        }
    
        private void LogRequest(object sender, EventArgs e)
        {
            //Log operation goes here
            HttpContext context = ((HttpApplication)sender).Context;
            string url = context.Request.Path;
    
            if (url.ToLower().EndsWith("pdf")) 
            {
                // log to google analytics
            }
        }
    
        public void Dispose()
        {
        }
    }
    

    Step 2 In the web.config system.webServer section add the line below:

    <add name="Files" type="HelloWorldModule"  />
    

    It should look like this:

    enter image description here

    Step 3

    Change the class above to send the pdf details to google analytics. There seems to be multiple ways to do this, have a look at the developer guide in the link below:

    https://developers.google.com/analytics/devguides/collection/protocol/v1/

    I hope this helps.

  • Mov3nforward 117 posts 319 karma points
    Feb 18, 2021 @ 14:03
    Mov3nforward
    0

    Do I just need to create a class called HelloWorldModule?

  • Brendan Rice 538 posts 1099 karma points
    Feb 18, 2021 @ 15:37
    Brendan Rice
    0

    Have you got .Net experience?

    It depends on how the solution is set up (web site/web application) on how you'll structure the code/project.

    If you create an App_Code folder in the root of the website and create the class in there it should work, regardless.

  • Mehmet Avcı 55 posts 240 karma points
    Feb 18, 2021 @ 18:53
    Mehmet Avcı
    1

    Hi,

    You can actually achive it via implementing either IHttpModule or IHttpHandler. I added info regarding handler below. This requires a couple changes.

    1. You need a class implementing IHttpHandler to grab requests
    2. You need to add required configuration entries in web.config depending what you want to track. In your case, since you want to track pdf files in Umbraco, you need to add it into media/web.config file, not the config file on the root.

    using System;
    using System.Web;
    namespace YourNamespace.App_Start.Handlers
    {
        public class FileHandler : IHttpHandler
        {
            public void ProcessRequest(HttpContext context)
            {
                switch (context.Request.HttpMethod)
                {
                    //First make sure this is a get request
                    case "GET":
                    {
                        //This will give you absolute path
                        var absolutePath = context.Request.Url.AbsolutePath.Trim();
    
                        /*
                         * Now you can check the path for pdf
                         * Verify your file if exist
                         * Store it as a counter at google analytics or somewhere in db
                         */
    
                        break;
                    }
                }
            }
    
            public bool IsReusable { get; }
        }
    }
    

    And then media/web.config file should look like this;

        <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
      <system.webServer>
        <handlers>
          <clear/>
          <add name="PDF"  path="*.pdf"  verb="*" type="Yournamespace.Handlers.FileHandler" />
    <!-- lines below should be there by default - at least on v7 -->
          <add name="StaticFileHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.StaticFileHandler"/>
          <add name="StaticFile" path="*" verb="*" modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule" resourceType="Either"
            requireAccess="Read"/>
        </handlers>
      </system.webServer>
    </configuration>
    

    PS: In a project we are pushing tracking data to google analytics with a specific category.

    Best, Mehmet

Please Sign in or register to post replies

Write your reply to:

Draft