javascript - jquery retrigger click after download -


I am trying to catch up with JQuery when a user listens or downloads my songs, and Gaul Analytics Generates an event for.

& lt; Audio> Element This works fine, but there is some discomfort when the user downloads the song.

The code is:

  $ ("audio"). Bind ("play", function (event) {ga ('send', 'event', 'audio', 'play', event.target.id);}); $ ("Audio"). Bind ("end", function (event) {ga ('send', 'event', 'audio', 'end', event.target.id);}); Click (function (event) {ga ('send', 'event', 'audio', 'download', event.target.id);});  

All play and end events are cached, as long as the user does not click on the "download" link for the first time. The first download event has been triggered correctly, but no more events have been detected since now, neither play or end. I also tried:

  $ (document) .on ("click", "a: contains ('download')", function (event) {ga ('send', 'event ',' Audio ',' download ', event.target.id);});  

with similar results.

I found a possible solution

The basic problem was that I Wanted to give a button to download songs, which opened the "save as" message box. My original code was HTML download to use the property:

  & lt; A href = "/ mp3 / mysong.mp3" downlaod> Download & lt; / A & gt;  

There were some problems with this solution:

  • not supported by all browsers
  • When a message box appears, then jQuery It seems that to stop listening to events in the page, so I can not do anymore (through Google Analytics) the number of times my user can play on my songs.

My solution is the following.

HTML : Define a class associated with the download event ( TrackDownload )

  & lt; Class = "TrackDownload" href = "/ MP3 / Mysong.mp3" & gt; Download & lt; / A & gt;  

JS : Join the class opening track download , opening a new frame, in this way the jQuery main page monitors any other song The page is downloaded

  $ (document) .ready (function () {$ ("a.Trackdownload"). Click (function (event) {ga ('send', $ ("Target"); $ ("target"); $ (this) .prop ('event', 'audio', 'download', event.target.id);}); $ ("a.Trackdownload" ARP '), href: $ (this) .press (' href ')} $ ("audio"). Bind ("play", function (event) {ga (' send ',' e Vent ',' audio ',' play ', event.target.id);});});  

.htaccess : at server level, this Define the fact that MP3 files are downloaded rather than for running, in this way a new frame is not opened, but instead changes in the "Save as box" message box.

  FilesMatch ".mp3 $" & gt; ifModule mod_headers.c & gt; Header set content-displacement "attachment" header set content type "app / octet-stream" & lt; / IfModule & gt; & Lt; / FilesMatch & gt;  

Perhaps more elegant solutions but it does one thing.


Comments

Popular posts from this blog

winforms - C# Form - Property Change -

java - Messages from .properties file do not display UTF-8 characters -

javascript - amcharts makechart not working -