Jquery approach can be implemented, but I'm afraid it will be exceedingly complicated, because ribbon buttons can change their size if you resize the browser window (so the id of the button will be changed respectively, for example from ..-Large
to ..-Medium
, etc.), and also they're created after page load, dynamically with js - so you will need some additional actions to run your script after the ribbon is created.
I recommend you to use a more native, SharePoint-way: custom actions. With custom action, you should replace OOTB button with your own button definition, run your script, and then, run the original command to keep the download functionality.
That can be done either through SharePoint Designer (but in this case you could not provide a correct tooltip), or using Visual Studio and Elements.xml.
Sample Elements.xml for Visual Studio (it should be added to a feature):
<CustomAction Id="TrackDownloadsAction" RegistrationType="List" RegistrationId="101" Location="CommandUI.Ribbon">
<CommandUIExtension>
<CommandUIDefinitions>
<CommandUIDefinition Location="Ribbon.Documents.Copies.Download">
<Button
Id="Ribbon.Documents.Copies.Download"
Sequence="10"
Command="TrackDownloads"
Image16by16="/_layouts/$Resources:core,Language;/images/formatmap16x16.png" Image16by16Top="-176" Image16by16Left="-144"
Image32by32="/_layouts/$Resources:core,Language;/images/formatmap32x32.png" Image32by32Top="0" Image32by32Left="-320"
ToolTipTitle="$Resources:core,cui_ButDownloadCopy;"
ToolTipDescription="$Resources:core,cui_STT_ButDownloadCopy;"
LabelText="$Resources:core,cui_ButDownloadCopy;"
TemplateAlias="o1"
/>
</CommandUIDefinition>
</CommandUIDefinitions>
<CommandUIHandlers>
<CommandUIHandler
Command="TrackDownloads"
CommandAction="javascript:alert('Hello World!'); SP.Ribbon.PageManager.get_instance().executeRootCommand('DownloadCopy', window.g_CUIcommandProperties, {CommandId: 'DownloadCopy'}, null);" />
</CommandUIHandlers>
</CommandUIExtension>
</CustomAction>
I've tested this and it works.
Btw, I've snooped the original button XML from C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\GLOBAL\XML\CMDUI.XML
file.