简体   繁体   中英

How to track files received from Request (asp.net mvc3) for Google Analytics?

I'm using ASP.NET MVC3 C# and I wrote in model a method which takes the files and archive them in a ZIP format like:

HttpContext.Current.Response.ContentType = "application/zip";
HttpContext.Current.Response.AddHeader( "content-disposition", "filename=" + myFilename );

 ZipForge zip = new ZipForge();
    try {
       zip.FileName = Path.Combine( folder, myFilename );
       zip.OpenArchive( System.IO.FileMode.Create );
       zip.BaseDir = Path.GetPathRoot( aPath );
       zip.Options.StorePath = StorePathMode.NoPath;

       zip.AddFiles( file1 );
       zip.AddFiles( file2 );

       zip.CloseArchive();

       HttpContext.Current.Response.WriteFile( zip.FileName );

    } catch {}

The ZIP file is wrote on Request and send back to original page where someone pressed on Download button.

I want to track this ZIP file for Google Analytics.

How to do that in this case ?

I read http://support.google.com/googleanalytics/bin/answer.py?hl=en&answer=55529 but in my case is too complicate.

I have to use a Javascript object in Controller ?

I need an advice Thank you

I read http://support.google.com/googleanalytics/bin/answer.py?hl=en&answer=55529 but in my case is too complicate.

Why? Simply generate the link that is pointing to your download controller action as explained in the article:

@Html.ActionLink(
    "download file", 
    "download", 
    "home", 
    null, 
    new { 
        onclick = "javascript: _gaq.push(['_trackPageview', '" + Url.Action("download", "home") + "']);" 
    }
)

or do it unobtrusively:

@Html.ActionLink(
    "download file", 
    "download", 
    "home", 
    null, 
    new { 
        id = "mydownloadlink"
    }
)

and then in a separate javascript file:

$(function() {
    $('#mydownloadlink').click(function() {
        _gaq.push(['_trackPageview', this.href]);
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM