Skip to content
Browse files

Adding more functionality

  • Loading branch information...
1 parent 2d0e0cc commit 185ad6d3a24e08c5a93ec8c68a6181db65cfd006 @briankirsten committed Mar 14, 2012
Showing with 193 additions and 8 deletions.
  1. +93 −6 src/js/cookiegen.js
  2. +24 −2 src/test/test.html
  3. +76 −0 src/track.php
View
99 src/js/cookiegen.js
@@ -10,7 +10,13 @@
var id = result.id;
- createCookie(tracker_config.cookiename, id, tracker_config.cookie_expiration);
+ if(id != null)
+ {
+ createCookie(tracker_config.cookiename, id, tracker_config.cookie_expiration);
+
+ trackItems();
+ }
+
}
function createCookie(name,value,days) {
@@ -22,6 +28,21 @@
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
+
+ function eraseCookie(name) {
+ createCookie(name,"",-1);
+ }
+
+ function getParameterByName( name ){
+ name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
+ var regexS = "[\\?&]"+name+"=([^&#]*)";
+ var regex = new RegExp( regexS );
+ var results = regex.exec( window.location.href );
+ if( results == null )
+ return "";
+ else
+ return decodeURIComponent(results[1].replace(/\+/g, " "));
+ }
function readCookie(name) {
var nameEQ = name + "=";
@@ -33,17 +54,83 @@
}
return null;
}
+
+ function trackItems(){
+
+ if(tracker_config.varsToTrack.length>0)
+ {
+ for (var i = 0; i < tracker_config.varsToTrack.length; i++)
+ {
+ var trackerVar = tracker_config.varsToTrack[i];
+
+ if(trackerVar.params.length > 0) {
+
+ var table = trackerVar.table;
+
+ var database = trackerVar.db;
+
+ var qs = "trk_name=" + tracker_config.cookiename + "&";
+
+ qs += "table=" + table + "&database=" + database + "&";
+
+ for (var j = 0; j < trackerVar.params.length; j++) {
+ var paramName = trackerVar.params[j];
+
+ var paramValue = getParameterByName(paramName);
+
+ qs += paramName + "=" + paramValue + "&";
+
+ }//end for
+
+ if(trackerVar.additionalVars.length>0){
+
+ for(var x = 0; x < trackerVar.additionalVars.length; x++){
+
+ var addVar = trackerVar.additionalVars[x];
- function eraseCookie(name) {
- createCookie(name,"",-1);
+ var key = addVar.key;
+
+ var value = addVar.value;
+
+ qs += key + "=" + value + "&";
+
+ if (typeof addVar.checkVal === "undefined")
+ {
+ //do nothing
+ } else {
+ if(addVar.checkVal == true)
+ {
+ //append the info to the service to make sure that tracking value is unique
+ qs += "checkVal=" + key +"|" + value + "&";
+ }
+ }
+
+ } //end for
+
+ } //end if
+
+ qs = qs.substring(0, qs.length-1);
+
+ document.write('<script src="'+ tracker_config.domain +'/track.php?'+ qs +'"><\/script>');
+
+ } //end if
+
+ }//end for
+ }//end if
}
function callFeed() {
+
+ var cookieVal = readCookie(tracker_config.cookiename);
+
+ if(cookieVal == null){
- if(readCookie(tracker_config.cookiename) == null){
- document.write('<script src="'+ tracker_config.domain +'/generator.php?callback=cookieGen"><\/script>');
+ document.write('<script src="'+ tracker_config.domain +'/generator.php?callback=cookieGen"><\/script>');
+
} else {
- //alert('else!');
+
+ trackItems();
+
}
}
View
26 src/test/test.html
@@ -15,15 +15,37 @@
var tracker_config = {
domain: 'http://localhost:8896',
cookiename: 'trkit_tracker',
- cookie_expiration: 365
+ cookie_expiration: 365,
+ varsToTrack : []
};
+
+ var affiliateTracker = {
+ table : "nm_affiliate_program",
+ db : "nm_affiliate_program",
+ params : [],
+ additionalVars : []
+ }
+
+ var addVar = {
+ key : "aff_program",
+ value : "initial",
+ checkVal : true
+ }
+
+ affiliateTracker.additionalVars.push(addVar);
+
+ affiliateTracker.params.push("hitid");
+
+ affiliateTracker.params.push("cid");
+
+ tracker_config.varsToTrack.push(affiliateTracker);
+
//-->
</script>
</head>
<body>
-
<script type="text/javascript" src="http://localhost:8896/js/cookiegen.js"></script>
</body>
</html>
View
76 src/track.php
@@ -0,0 +1,76 @@
+<?php
+
+date_default_timezone_set('America/New_York');
+
+header('Content-type: application/json');
+
+$trk_name = "";
+
+$cookieVal = "";
+
+$database = "";
+
+$table = "";
+
+$checkVal = Array();
+
+$paramArray = Array();
+
+foreach($_REQUEST as $key => $value)
+{
+ if($key == "trk_name")
+ {
+ $trk_name = $value;
+ } else if(($trk_name != "") && ($key == $trk_name))
+ {
+ $cookieVal = $value;
+ } else if($key == "database")
+ {
+ $database = $value;
+ } else if($key == "table")
+ {
+ $table = $value;
+ } else if($key == "checkVal"){
+
+ $checkArray = explode("|", $value);
+
+ $theKey = $checkArray[0];
+
+ $theValue = $checkArray[1];
+
+ $checkVal["key"] = $theKey;
+
+ $checkVal["value"] = $theValue;
+
+ } else {
+
+ $paramArray[$key] = $value;
+ }
+
+}
+
+$con = new Mongo("mongodb://127.0.0.1"); // Connect to Mongo Server
+$db = $con->selectDB($database); // Connect to Database
+$coll = $db->selectCollection($table); //Select the collection
+
+$autoReturn = $coll->findOne(array($trk_name => $cookieVal, $checkVal["key"] => $checkVal["value"]));
+
+if(is_null($autoReturn)){
+
+ $trackObj = array($trk_name=>$cookieVal);
+
+ foreach($paramArray as $key => $value)
+ {
+ $trackObj[$key] = $value;
+ }
+
+ $trackObj["ts"] = new MongoDate();
+
+ $coll->insert($trackObj);
+
+}
+
+$con->close();
+
+
+echo "{\"status\":\"success\"}";

0 comments on commit 185ad6d

Please sign in to comment.
Something went wrong with that request. Please try again.