Best way to get notified when a user clicks on a specific link

Hello,

I have been asked to be able to get notified whenever a user clicks on a link to download a software we provide through the wiki. More specifically we provide a release notes page with a button that is a link to an attached file to this page. I first looked into the statistics module but it is not detailed enough to give this level of information.
I am now thinking about using a velocity macro to detect each time a user clicks the button, and the idea behind is that the macro would send an email whose content would be the url of the page and the name of the user.
I am new to velocity programming, but I would just like to know if I am pointing in the right direction here, and if this is the best solution to get the information I need or if there are simpler ways. Any advice welcome 

Thanks,

Yann

Easy solution:

  • Generate a link to a special page, with a query string param containing the link to redirect to
  • On that special page, send your email using a scripting language and the xwiki mail sender api
  • Redirect to the query string param
1 Like

Hello Vincent,

Thanks a lot for these smart guidelines! I am almost there. I am just struggling with the third point “redirect to the query string param”. Actually the redirection works fine if the string param is a page, but fails if the link points to an attachment. I tried different solutions, but all of them failed.

Here is what works when the param is a page :

On the source page I created this link (the param is named ‘link’, the special page DownloadNotificationMacro ) :

[[[[image:FileManager.tĂ©lĂ©charger.png@tĂ©lĂ©charger.png]]>>doc:.DownloadNotificationMacro.WebHome||queryString=“link=doc:Sandbox.TestPage3”]]

On the special page :

#set($downloadlink = $request.get(“link”))


#code for sending the email

.

$response.sendRedirect($xwiki.getURL($downloadlink))

Here is what fails when the param is an attachment :

On the source page I created this link (the param is named ‘link’) :

[[lien de test>>doc:.DownloadNotificationMacro.WebHome||queryString=“link=attach:Sandbox.TestPage3.watchlist_1.png”]]

Redirection brings me to 
/xwiki/bin/view/Sandbox/TestPage3/watchlist_1/png

The correct link is 
/xwiki/bin/download/Sandbox/TestPage3/watchlist_1.png

Several problems here (‘view’ instead of ‘download’, and the dot of the filename is replaced by a /)

I tried percent encoding, pasting the correct link, escaping the dot with an ,
 but could not find a working solution. Maybe the use of $xwiki.getURL() is not the way to go in this particular case? I tried to read the documentation in order to find a better suited function, but I could not.

Thanks again for your help, and sorry for those novice questions.

Yann

Several points:

  • Your query string param is incorrect. It should be a reference to an attachment, i.e. attachmentReference=Sandbox.TestPage3@watchlist_1.png.
  • You then need to convert the String into an AttachmentReference, using $set ($attachmentReference = $services.model.resolveAttachment($attachmentReferenceString))
  • You can then use $xwiki.getURL($attachmentReference) to get the URL

See also http://extensions.xwiki.org/xwiki/bin/view/Extension/Model%20Module#HScriptServiceAPI

Hope it helps

Thanks it works fine now :slight_smile: I was missing the conversion part


Thanks again, really appreciated


As a side note, I ran into one last issue that I resolved, I just wanted to document it :

  • as the path to the attachments contained a directory with dots (in the form of a version number x.y.z as the attachments are software versions stored in the extension release notes) the redirection failed because all the dots where transformed in /. So I escaped the dots inside the x.y.z directory with \ percent encoded

=> x%5C.y%5C.z

Maybe not the most elegant solution, but it works fine.

Yann

This is not correct. You shouldn’t escape anything. The APIs that I linked do the escaping automatically. If not then there’s a big bug that we need to fix and it needs to be reported. Thx

Hello Vincent,

Actually I tried different things. Before reporting a potential bug, I prefer pasting my code as I am not so confident that I understood perfectly how to use the APIs :

On the release notes page :

[[[[image:oabstore:FileManager.télécharger\.png@télécharger.png]]>>doc:ReleaseNotes.Code.DownloadNotification.WebHome||queryString="link=ReleaseNotes.Data.Email.2017%5C.11%5C.02.WebHome@Livraison_Email_20171102.zip"]]

On the special page DownloadNotification :

#set($attachmentRefString = $request.get("link"))
#set($attachmentRef = $services.model.resolveAttachment($attachmentRefString))

... mail sending

$response.sendRedirect($xwiki.getURL($attachmentRef))

This works.

If I don’t escape the dots in the link parameter the redirection sends me the link :
http://xwiki.hostname/xwiki/bin/download/ReleaseNotes/Data/Email/2017/11/02/WebHome/Livraison_Email_20171102.zip
which of course does not exist.

I also tried to create the attachmentreference right from the original page wich failed too.

If I did not use the api properly I am really sorry. If not I will be happy to report a defect.

Thanks,

Yann

ok so the error is that the syntax you’re using for the reference is not correct since the dots are not escaped (in the link query string param), see http://extensions.xwiki.org/xwiki/bin/view/Extension/Model%20Module#HDocumentReferences.

If the reference is static (it’s not computed) then you simply need to escape the dots as in:

[[[[image:oabstore:FileManager.télécharger\.png@télécharger.png]]>>doc:ReleaseNotes.Code.DownloadNotification.WebHome||queryString="link=ReleaseNotes.Data.Email.2017%5C\.11%5C\.02.WebHome@Livraison_Email_20171102.zip"]]

(I’ve escaped the dots for 2017.11.20).

If the reference is computed then you need to create an AttachmentReference object and serialize it with $services.model.serialize($attachmentReference).

Thank you Vincent, I’ll stick to the static reference.