APEXblog.nl - Tips and Tricks

About This Blog:
I (Richard Weug) started this blog primary for myself to save all kinds of Apex tips and tricks in one place. To use it as a place to write down how I used some coding in my own projects, but also to copy and paste all kinds of articles I find on the Internet. (So I never have to wonder on what website did I read about??? When I see something interesting I collect the content so I have my own archive/knowlegde base.

View my profile on LinkedIn

 

Tips & Tricks

Print
26
May

Count URL clicks

Written by Richard Weug. Posted in Tips and Tricks

This is a demonstration of using a two-step dynamic action to track how recently link columns from an APEX report have been clicked, based on a request from a lady I met at the KScope11 conference. The report is based on the following table:

?
1
2
3
4
5
6
7
8
create table demo_rpt_click_tracking (
    rpt_id number primary key,
    name varchar2(25) unique,
    url varchar2(255) not null,
    is_local varchar2(1) default 'N' not null,
    last_click date,
    click_count number default not null
);

From this, I created a basic interactive report, hiding the rpt_id and url columns. The name column is a link, with the following fields set:

  • Link Text: #NAME#
  • Link Attributes: id="ct_#RPT_ID#"
  • Target: URL
  • URL: #URL#

The end result when the page is generated is something like this:

?
1
<a href="http://google.com" id="ct_1">Google</a>

Next, I created a hidden page item (P1_CLICKED_URL), with its source set to always null. Finally, I was ready to create the dynamic action, attached to the Click event on the jQuery selector "a[id^=ct_]". This ensures that I'm only attaching the dynamic action to the URLs in the report. The first action is of "Execute JavaScript Code" type:

?
1
$s('P1_CLICKED_URL',this.triggeringElement.id);

All this does is set the hidden page item to hold the ID of the URL the user clicked on. The second action is of "Execute PL/SQL Code" type:

?
1
2
3
4
5
6
7
8
9
10
11
declare
  l_rpt_id number;
begin
  l_rpt_id := to_number(substr(v('P1_CLICKED_URL'),4));
 
  if l_rpt_id is not null then
    update demo_rpt_click_tracking
    set last_click = sysdate, click_count = click_count+1
    where rpt_id = l_rpt_id;
  end if;
end;

Note that it also submits the P1_CLICKED_URL page item. With this in place, we now have visibility to the exact time a link has been clicked. Oh, and make sure that neither step of the dynamic action is set to run when the page loads, or you may get weird results. Also, note that this demonstration only tracks when the user left-clicks on the URL; if they use the right-click/open in new window menu, it does not get trapped.

Original article: http://www.davidsgale.com/pls/apex//f?p=190:1:0