1

I have a button on my page to open inserted link in a new tab. To be able to track visit and if visitor clicked that button, I am trying to add variable to onclick function.

Here is my button:

<a href="javascript:void(0);"  onClick="window.open('$link','_blank')">
<button type="button" class="btnbuy">Buy</button></a>

Here is what I am trying to do:

<a href="javascript:void(0);"  onClick="myFunction()">
<button type="button" class="btnbuy">Buy</button></a>

<script>
function myFunction() {
if(window.open("$link", "_blank")){
$isclicked = true;
} }
</script>

$sql=mysql_query("INSERT INTO stats (id,isclicked) VALUES ("","$isclicked"");

I just wrote this function to show what I am trying to do, so please don't care syntax errors.

Am I on the right way or should I try something else? Thanks for your helps.

kazata
  • 71
  • 2
  • 2
  • 10

5 Answers5

0

No need to put an if() around any of it.

Just do all your logic prior to the window.open() call.

Ohgodwhy
  • 49,779
  • 11
  • 80
  • 110
0

I would suggest that you use JQuery instead of the onclick function.

That would mean your HTML can shorten down to

<button type="button" class="btnbuy">Buy</button>

Your JQuery would be something like

$(".btnbuy").on('click',function(){
alert("clicked")
/*AJAX CALL to update the value in the database*/
});

Using the onclick method isn't the best .Check link for more..

Regarding updating the counter, you can simply have an AJAX call that updates the value in the data base.This will also help you write better error or success messages with the .then syntax.

Community
  • 1
  • 1
Satej S
  • 2,113
  • 1
  • 16
  • 22
0

$(".btnbuy").on("click",function(){

  window.open("<?= $link?>", "_blank")
  $.ajax({
  
  url:'somephpfile.php'
  success:function(data){
  alert(data)
  }
   
  
  });


})
In somephpfile.php

$sql=mysql_query("INSERT INTO stats (id,isclicked) VALUES ("",'true');
echo "success";
Naisa purushotham
  • 905
  • 10
  • 18
0

Use google analytics and add it as a new dimension.
This way you will be able to track it far better than just a number.

Andreas
  • 23,610
  • 6
  • 30
  • 62
  • Actually, I am using google analytics for my page. However, I am trying to do my own analytics with basic graphs to add these graps into my CRM for Boss:) Just to make her understand better. – kazata Apr 13 '16 at 07:28
  • @kazata then teach your boss how to use google analytics ;-) ///// ok, I see your point – Andreas Apr 13 '16 at 09:33
0

I'd recommend usning jQuery

Remove the href-line, so you only have your <button> defined

Next, include the jQuery lib

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

in your <head>

Then write a function like this:

$(".btnbuy").on('click',function(){
   window.open("$link", "_blank")){
     $isclicked = true;
    }
  });
}
pguetschow
  • 5,176
  • 6
  • 32
  • 45