Expand my Community achievements bar.

Track how many time a link is clicked by Analytics via Launch.

Avatar

Level 2

Hi Guys, 

 

I'm having this problem with tracking the click times with though launch. There are 2 buttons on the page that contain the PDF url that I want to track: 

<a href="/content/document-1.pdf" target="_blank">

      <button class="button" style="text-align: left;padding: 10.0px 5.0px 10.0px;float: left;margin: 2.0px;">Button 1<br> </button></a>

<a href="/content/document-2.pdf" target="_blank">

      <button class="button" style="text-align: left;padding: 10.0px 5.0px 10.0px;float: left;margin: 2.0px;">Button 2<br> </button></a>

 

The setup we have here is using launch to inject the tracking code in to a webpage. This code contain an onclick event that will get all the href in the page and select only the clicked parent href of the button that has been clicked. The code is listed below: 

 

var btns = document.querySelectorAll('a[href] button');

var PDFTrack;

function GetPDFUrl(){
console.log("~~~~~~~~~~~~~~~~~~~~Rules~~~~~~~~~~~~~~~~");
if( this.parentElement.getAttribute('href').includes('.pdf') ) {
PDFTrack = "not set";
delete window.last_clicked_href;
console.log("PDF and lastclickhref: " + PDFTrack + " " + window.last_clicked_href);

PDFTrack = this.parentElement.getAttribute('href');
console.log("PDF track: " + PDFTrack);
window.last_clicked_href = decodeURI(PDFTrack.substring(PDFTrack.lastIndexOf("/") + 1));
console.log("window.lastClick: " + window.last_clicked_href);
}
console.log("~~~~~~~~~~~~~~~~~~END OF RULES~~~~~~~~~~~~");
}

console.log("For loops trigger");
for (var i = 0; i < btns.length; i++){
var btn = btns[i];
btn.addEventListener('click', GetPDFUrl);
}
return true;

 

The setup I got is set up in rule, That is shown below: 

EVENTS
CLICK

CONDITIONS
Domain AND Custom Code

ACTIONS
Abobe Analytics - send Beacon THEN Abode Analytics - Clear Variables THEN Clear JS var

 

The Click event only catch tag a which contain tag href with property (.pdf)

The Custom code condition is listed above. The purpose of the code is to set the variable and map it to the Data element which will be sent by "Abobe Analytics - send Beacon"

 

The result I got when load the page and click the button-1 for the 1st and 2nd is:

DOWNLOAD LINK : <button class="button" style="text-align: left;padding: 10.0px 5.0px 10.0px;float: left;margin: 2.0px;">View Poster<br> </button>
Report Suite ID : xxxxxx
Current URL : https://abc/download-page.html
Currency Code : USD
Char Set : UTF-8
Activity Map
page: https://abc/download-page.html
link: <button class="button" style="text-align: left;padding: 10.0px 5.0px 10.0px;float: left;margin: 2.0px;">View Poster<br> </button
region: BODY

but when I click the button 3rd time, it show the correct value 

DOWNLOAD LINK : document-1.pdf
Report Suite ID : xxxxxx
Current URL : https://abc/download-page.html
Currency Code : USD
Char Set : UTF-8
Activity Map
page: https://abc/download-page.html
link: document-1.pdf
region: BODY

And when I switch to click button-2 the first time, it still show the data from button-1, only when I click the button-2 again, the call out then show the correct data.

DOWNLOAD LINK : document-2.pdf
Report Suite ID : xxxxxx
Current URL : https://abc/download-page.html
Currency Code : USD
Char Set : UTF-8
Activity Map
page: https://abc/download-page.html
link: document-2.pdf
region: BODY

The variables from the custom code did return the correct value, but some how did not get assign to the "link" and "Download Link" in the call out. Could some one help me on this. 

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

0 Replies