PDA

View Full Version : Ajax - Trying to count thread views on outgoing links.


neverstop
03-15-2008, 03:31 AM
Hi,

I will preface by saying I am a total noob and I really know nothing.

I am trying to use one of my forums as a link dump/directory. I am trying to count the thread views on an outbound link (ie clicking the link increases thread views by 1). I found a snippet of code on another forum and I am trying to get it to fill my needs:

<script type="text/javascript">
function trackClick( id ){
var url = "http://www.amateurpornster.com/showthread.php?t=" + id;
xmlRequestObj = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP")
xmlRequestObj.open("GET", url, true);
xmlRequestObj.send(null);
return false;
}
</script>

So I am using a link like this in the threadbit template (in this forum (3) I will just put the URL in as the post body so $thread[preview] becomes the URL):

<a href=<if condition="in_array($forumid, array(3))">"$thread[preview]" onClick="trackClick( '$thread[threadid]' );" target="_blank"<else />"showthread.php?$session[sessionurl]t=$thread[threadid]"</if> id="thread_title_$thread[realthreadid]"<if condition="$show['gotonewpost']"> style="font-weight:bold"</if>>$thread[threadtitle]</a>

Now I may be completely off, but should that work for counting the views when the trackClick function is triggered by clicking on the link?

Any help would be appreciated, or just tell me it wont work and I will look for another solution!

Cheers,
Ian

SEOvB
03-15-2008, 06:06 AM
No clue, but Google Analyitcs provides this information and its free and a lot easier to implement

Dismounted
03-15-2008, 06:06 AM
But then you'll get an increase on the thread view by 2 (one for entering thread, one for clicking link), and this isn't what you want, right?

neverstop
03-15-2008, 06:42 AM
No clue, but Google Analyitcs provides this information and its free and a lot easier to implement

Oh yeah? Can you elaborate please?

But then you'll get an increase on the thread view by 2 (one for entering thread, one for clicking link), and this isn't what you want, right?

Not exactly...

When they click the thread title link they wont be taken to the thread:

<a href=<if condition="in_array($forumid, array(3))">"$thread[preview]" onClick="trackClick( '$thread[threadid]' );" target="_blank"<else /> ...

$thread[preview] = the URL i want to go to. It all works but except the function isn't working afaik (ie: the views arent going up).

--------------- Added 1205605006 at 1205605006 ---------------

Ok my mistake, this seems to be working. Since my board is not live yet I am the only one on the site so the cron to count thread views wasnt getting triggered.

Now that I know the function is working, I don't think showthread.php is appropriate for this when I can have a much smaller file accomplish the same.

Can anyone show me how to write a file that would basically:

UPDATE " . TABLE_PREFIX . "thread
SET views = views + 1
WHERE threadid = the thread id that was clicked

Cheers,
Ian

neverstop
03-17-2008, 07:27 PM
Can anyone show me how to write a file that would basically:

UPDATE " . TABLE_PREFIX . "thread
SET views = views + 1
WHERE threadid = the thread id that was clicked

Cheers,
Ian

Bump...

Basically I want to make a file (track.php) that when clicked adds a view to the thread table. So clicking on track.php?id=15 would update thread 15 with one more view. Not sure if its possible to have it update hourly like regular thread views?

Can someone help me out on this one pls.

Thanks in advance,
Ian

MoT3rror
03-17-2008, 08:57 PM
Here is the php code for it
// update views counter
if ($vbulletin->options['threadviewslive'])
{
// doing it as they happen; for optimization purposes, this cannot use a DM!
$db->shutdown_query("
UPDATE " . TABLE_PREFIX . "thread
SET views = views + 1
WHERE threadid = " . intval($threadinfo['threadid'])
);
}
else
{
// or doing it once an hour
$db->shutdown_query("
INSERT INTO " . TABLE_PREFIX . "threadviews (threadid)
VALUES (" . intval($threadinfo['threadid']) . ')'
);
}


You can find that in showthread.php

neverstop
03-18-2008, 04:10 AM
Here is the php code for it
// update views counter
if ($vbulletin->options['threadviewslive'])
{
// doing it as they happen; for optimization purposes, this cannot use a DM!
$db->shutdown_query("
UPDATE " . TABLE_PREFIX . "thread
SET views = views + 1
WHERE threadid = " . intval($threadinfo['threadid'])
);
}
else
{
// or doing it once an hour
$db->shutdown_query("
INSERT INTO " . TABLE_PREFIX . "threadviews (threadid)
VALUES (" . intval($threadinfo['threadid']) . ')'
);
}


You can find that in showthread.php

OK cool. Now I am a beginner so please bear with me here. How do I make a new file with that? Where it would take the id (track.php?id=x)from the url and update the table?

Cheers,
Ian

Dismounted
03-18-2008, 04:49 AM
<?php

// setup vbulletin backend
require_once('./global.php');

// clean id
$threadid = intval($_GET['id']);

// exit if empty id
if (!$threadid)
{
exit;
}

// update views counter
if ($vbulletin->options['threadviewslive'])
{
// doing it as they happen; for optimization purposes, this cannot use a DM!
$db->shutdown_query("
UPDATE " . TABLE_PREFIX . "thread
SET views = views + 1
WHERE threadid = $threadid
");
}
else
{
// or doing it once an hour
$db->shutdown_query("
INSERT INTO " . TABLE_PREFIX . "threadviews
(threadid)
VALUES
($threadid)
");
}

?>
Put it in a file called track.php and upload it.

neverstop
03-18-2008, 02:22 PM
Hmmm, the above code does not seem to be working, the views are not being added. If I go to the file in my browser (ie:www.mydomain.com/track.php?id=15) I get a white screen (that is expected I guess) and there is no error or anything, views not being added. Tried changing the admincp option to both hourly and instant but no changes.

Cheers

Dismounted
03-19-2008, 05:12 AM
Please try this and report the output.
<?php

// setup vbulletin backend
require_once('./global.php');

// clean id
$threadid = intval($_GET['id']);

// exit if empty id
if (!$threadid)
{
die('Invalid Thread ID');
}

// update views counter
if ($vbulletin->options['threadviewslive'])
{
// doing it as they happen; for optimization purposes, this cannot use a DM!
$db->shutdown_query("
UPDATE " . TABLE_PREFIX . "thread
SET views = views + 1
WHERE threadid = $threadid
");
}
else
{
// or doing it once an hour
$db->shutdown_query("
INSERT INTO " . TABLE_PREFIX . "threadviews
(threadid)
VALUES
($threadid)
");
}

die('Thread View Added');
?>

neverstop
03-19-2008, 06:00 AM
I get the "Thread View Added" message but there has not been a view added in the db. The threadview table is empty as I have it set to auto update in the admincp and no thread views are being added in the thread table.

neverstop
03-21-2008, 09:27 PM
Bump...

Can I ask any of you fine folks to try this script on their own site and see if a thread view gets added? I am doing this on a 3.7 board and I cannot figure out why a view is not added to the db...

Cheers

neverstop
04-07-2008, 05:09 PM
Another bump...

I have now tried this on a 3.6.x board just to be sure and the track.php script is not working on either 3.6 or 3.7. Can someone please have a look for me? This seems so simple, I can't see why it does not work.

Thanks in advance