Forums » Developers

Using PHP to grab Unique ID of visitors


I am using clicky on a wordpress site utilizing the Yoast Clicky Plugin.

I use a form on my site which tracks certain information via hidden fields and submits it to a custom database.

Basically when a visitor is on my website, I am trying to grab the "Unique ID" of that visitor so I can have that information submitted with my form.

What is the proper way of grabbing the "Unique ID" variable using php or javascript?

Thank you.

Posted Wed Oct 2 2013 8:54a by hosteddia***

You can't as the unique id is stored in a cookie set on domain and cross domain cookie reading is forbidden by every internet browser

Posted Wed Oct 2 2013 2:06p by pageaccu***

Yeah it's not possible because we use third party cookies for tracking when possible. There's also a first part cookie, _jsuid, that you could try to access - however in most cases, it won't be the third party _jsuid we store on our end. It will only match if they have third party cookies disabled, OR, if your site is the first site they have ever visited that has Clicky installed (unlikely!).

Posted Wed Oct 2 2013 2:33p by Your Friendly Clicky Admin

I would suggest just logging their IP instead. At the very least, with that, you will be able to find that one specific session on Clicky.

Posted Wed Oct 2 2013 2:33p by Your Friendly Clicky Admin


Posted Wed Oct 2 2013 3:14p by pageaccu***

Perhaps he can use the API to "reverse" the API to the unique visitor ID ?

Posted Wed Oct 2 2013 3:14p by pageaccu***

Supposing the visitor session has been processed, then that could potentially work but still nothing something I woudl necessarily recommend, unless desperate. Reason it may not have been processed yet is we process in batches once per minute.

Posted Wed Oct 2 2013 3:48p by Your Friendly Clicky Admin

Thank you for the suggestions and responses. My end result is just a way to track a returning user with clicky so if they submit a web form at a later date, we can view their details within clicky to see how many times they been to the website and all the other data that clicky provides. Was hoping the wordpress plugin provided some type of method, but I will continue to look into it. If anyone has any suggestions or links that might help out, that would be great.

Posted Wed Oct 2 2013 9:56p by hosteddia***

What would be recommended method if I wanted to be able to collect some type of unique information to map it to other analytic software? I am still trying to figure this out. Basically I have hidden fields on my contact form which submits data to my database. However once a user submits data, I am unsure how to map them so that I use the data submitted in the form, to track them in Clicky so I can view more stats about the user.

I do not believe I am going about this properly and may be overlooking a key element as I am sure I can not be the only person trying to map users who submit a form with collected analytics in Clicky...

Thank you in advance for any and all suggestions/comments?

Posted Sun Oct 13 2013 11:35a by hosteddia***


you have described a requirement to obtain a unique user id from clicky and store it in your application database so that you could later go into clicky using that identifier which was logged in your application database to pull queries about a user who filled out the form on a returning visit to your site.

An easy method to do this which would probably already be supported by a wordpress clicky plug-in might be to set a goal in clicky for the completion of your form. Perhaps you already have set a goal for this and I am missing your point.

But if not, then setting up a goal in clicky for the completion of this form will give you a rich set of built-in reporting about visitors who reached the goal. The goal would be a single identifier for all visitors, but when you query based on this goal identifier, the info you will get is the complete list of visitors who filled out the form. Then as you click on each visitor, clicky will present info about current visit and first visit (for those who are returning).

Hmmm... as I compose this post I am walking through this on clicky for one of my own sites, and I may have stumbled upon your unique requirement: which is to know that a visitor met the goal on a return visit versus first visit. I see that once you click on a visitor you can see info about first visit versus current visit, but I don't know how to query for ONLY those visitors who have met the goal after multiple visits to the site. Maybe that is what you were after.

If so, then I hope someone might see this elaboration of the requirement and comment on a simple way to pull up this info.

I can imagine a way to do it which would involve your site setting a cookie in order to recognize a returning visitor, and then if that visitor fills out the form on a return visit, to log a dynamic goal on clicky. You can log two goals for the visitor: first is they filled out the form, and second is they did it while on a return visit to your site. Then you could go query the second goal in clicky and it would report only the returning visitors who filled out the form. Look under Goals in clicky and they show you a sample javascript code to capture a dynamic goal. The code to implement the cookie to know the visitor is returning is outside of clickly, and beyond the scope of this post. Just wanted to offer that as an idea you might pursue which might not be very difficult to implement.

Posted Mon Oct 14 2013 4:50p by edyod***


> What would be recommended method if I wanted to be able to collect some type of
> unique information to map it to other analytic software?

This is not official from anyone, but it is how I do it. Feel free to copy, modify, and reuse this approach and the code provided below to suit your needs.

The Clicky team provides lots of different APIs you can use to enter and query clicky data. Using the clicky_log php function, I came up with a scheme to uniquely identify every visitor using a site identifier and time stamp.

This provides a capability to capture and correlate information about each unique visitor session. Clicky provides a Custom tracking capability which is supported in the Clicky reporting web site and reporting APIs.

Because the unique ID is generated in PHP code, the variable it defines persists and can be stored in a database, passed to Clicky, passed to affiliate tracking systems, other analytics systems, etc. to correlate visitor sessions with other data.

In my case, this unique ID is captured in a custom field in Clicky. I also pass it to other analytic systems to tag the visitor session. Then later I can export data from the various systems and correlate visitor information in a spreadsheet.

My requirement was for a fixed length unique ID that I could pass to other systems and that could be parsed by analytic tools to unpack whatever info I packed into the unique ID field. Currently I only pack a site identifier and a time stamp, but it would be easy to add more info as the need arises.

This unique ID as implemented in the sample code (see below) takes the following form:

-------- |___ three hex chars represent the fractional second portion of a php microtime() time stamp
---- |______ eight hex chars represent the whole seconds portion of a php microtime() time stamp
|______________ four char site identifier (I use this to track multiple web sites)

note that I add .1 to the fractional seconds value in order to ensure that it always requires precisely three hex digits. So it's not purely the fractional seconds but it is sufficiently unique. You can derive the human readable (decimal) time stamp if desired by reversing the code and passing it back through a php time function. Also you could choose to pack a human readable time stamp in the visitor_id instead of a hex number, it was simply how I chose to implement it.

hope you find this helpful to copy and use or at least to get you started on a scheme of your own.

The php code required to implement this unique visitor id includes the following two parts:

part 1 - the clicky_log function provided by clicky team

get it here:

part 2 - the custom field capture PHP code

___start of custom php code____
# this procedure generates a unique 11 character visitor tracking ID in $visitor_id then logs it to clicky
# the $visitor_id variable remains defined for use in PHP scripts so that it can be passed to other systems as needed


# $site_name in my case is defined by the calling PHP procedure and so this piece of code
# can be used on multiple web sites without modification.
# $site_name = "mywb"; # or whatever you want, not constrained to four chars but that was my convention

#echo "nn 'microtime' - Unique time stamp nn ";
$timestring = microtime();
#echo "microtime= #",$timestring,"#n";
# split timestring into two integer numbers
$pieces = explode(" ", substr($timestring, 2));
$pieces[0] = "1". substr($pieces[0],0,3); # results in .1xxx so the hex is always the same 3 char length for any fraction of a second
#echo "fracsec= #",$pieces[0],"#n";
#echo "epochsec= #",$pieces[1],"#n";
#echo "dechex = #", dechex($pieces[1]),"..",dechex($pieces[0]), "# ", strlen(dechex($pieces[1])), " .. ", strlen(dechex($pieces[0])), "n";
#echo "nn";
$visitor_id = $site_name . dechex($pieces[1]) . dechex($pieces[0]); # epoch seconds (8 hex chars) plus fractional second (3 hex chars)
#echo "nnvisitor_id = #" . $visitor_id . "#n";

#log the visitor id to clicky - function will pick up the visitor IP and associate specified $visitor_id with the visitor session
clicky_log( array( "type" => "custom", "custom" => array( "visitor_id" => $visitor_id )));

__end of custom php code___

Posted Mon Oct 14 2013 9:54p by edyod***

Thank you for all the replies. I have been busy and that is the cause of my delay.

@edyodis, you are a genius!!! I have a quick question though. As the $visitor_id is timestamp based, would that visitor ID change every time the user visited a new page or left the site and came back? The ID changing so often, how do you prevent clicky and your other analytics from making multiple visitor ids per visitor? I totally understand if that is to much to explain, if you do prevent that... but I was just curious if you do or does it not currently matter at the moment?

I was thinking of using php to create a cookie with the visitor id your code generates, and then just have it run whenever the user did not have an existing cookie.

Thank you again!

Posted Sat Oct 26 2013 8:43p by hosteddia***

hey hosteddialer,

thanks for the kind words.

yes, great observation. You only need to capture this visitor_id one time per visitor session, and clicky will correlate all of the visitor actions for that session with the visitor_id.

What I actually do with this visitor_id is capture it on a certain page and not all pages. Clicky automatically associates all actions for the entire visitor session with this visitor_id that I logged only one time.

(Now that you mention it, I am making an assumption that when they hit this page and get tagged, they won't be back, hmmm. If they hit the back button I am okay, but if they bop around and come back from another page, then I guess they will get a new visitor_id using my current approach. I bet what happens based on what data I do see is the last visitor_id logged overlays all the previous ones for that same session in clicky. So it probably doesn't hurt anything, but I don't know for sure. Never thought of this before you mentioned it and I had to think through it to write to you. I may need to test that in my own process flow, thanks for the heads up!)

Back on point, in my case I clicky_log the visitor_id when the user takes certain actions I want to track in an external system to clicky. This way I leave a breadcrumb in clicky that I can later associate with the same visitor_id which I also passed to an external system. And you know the rest... use a spreadsheet to mash up the data dump from external system and a clicky report.

Using my approach, I do NOT capture the visitor_id for visitors who do NOT take those certain actions I care about. So I want to capture visitor_id for people who did what I want, and use it to link all the info I can correlate about what led them to do it. For people who go away without taking any of the actions I want to track, I actually don't clicky_log a visitor_id for those people. You may or may not want to track all of them.

In your case, you describe a form with some hidden fields you can populate to capture the visitor_id in a database to correlate later with clicky data. Without knowing too much about your application, it makes sense to generate the visitor_id on that page where that form is, and capture it in both clicky and your database when the user (or the web page) submits the form.

If you wanted to use a cookie to keep visitor_id constant across multiple page views by the same user, you might do something like this...

if (isset($_COOKIE["visitor_id"])) {
$visitor_id = $_COOKIE["visitor_id"];
setcookie("visitor_id",$visitor_id,time() + 60*60*24*30); # push out expiration to 30 days from now
# print "Return Visitor " . $visitor_id . "nn";
} else {
setcookie("visitor_id",$visitor_id,time() + 60*60*24*30);
# print "New Visitor " . $visitor_id . "nn";

That will continue to preserve the original visitor_id for each return visit from the same computer & web browser with an expiration of 30 days following the last visit.

Well, I'm sure you are way ahead of me on how you might be able to use this kind of tracking.

best wishes,


Posted Mon Oct 28 2013 4:02p by edyod***

p.s. I left off a closing } in the php cookie code example.

Posted Mon Oct 28 2013 4:06p by edyod***

You must be logged in to your account to post!