广告点击js实现一例
为了统计广告投放效果,需要记录广告页投放后的点击数,二跳数,注册量。
广告页html代码如下,里面关键是埋了一个广告来源标识uid的hidden域,在页面加载后将url后面带的uid写入到该域中,达到注册可以记录来源的目的。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>adstat</title>
</head>
<body>
<div class="reg">
<form action="" method="POST">
<p> 用户名:<input name="user_name" type="text" maxlength="20" /></p>
<p> 用户密码:<input name="password" type="password" maxlength="20" /></p>
<p> 确认密码:<input name="repassword" type="password" maxlength="20" /></p>
<p><input type="hidden" name="uid" id="uid" value="" /></p>
<p><input type="button" value="" onclick="onSubmit();"/></p>
</form>
</div>
</body>
</html>
<script src="http://ad.xxx.com/js/testclk.js" type="text/javascript"></script>
testclk.js代码如下
var adstat = function(){
getCookie = function(name){
var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
if(arr != null) return unescape(arr[2]); return null;
},
setCookie = function(name, value, expires, path, domain, secure){
var today = new Date();
today.setTime(today.getTime());
if(expires){
expires = expires * 1000 * 60;
}
var expires_date = new Date(today.getTime() + (expires));
document.cookie = name + "=" +escape( value ) +
((expires) ? ";expires=" + expires_date.toGMTString() : "") +
((path) ? ";path=" + path : "") +
((domain) ? ";domain=" + domain : "") +
((secure) ? ";secure" : "");
},
secondClicks = function(){//二跳函数
clickFunc(2);
},
clickFunc = function(type){
var cookieName = 'one';
var cookieName2 = 'sencond';
var domain = document.domain;
var url = location.search;
var paramObj = new Object();
//解析url后面带的推广参数
if (url.indexOf("?") != -1){
var str = url.substr(1);
str = str.replace('\?','\&');
arr = str.split("&");
for(var i = 0; i < arr.length; i ++) {
paramObj[arr[i].split("=")[0]] = unescape(arr[i].split("=")[1]);
}
}
//赋值给页面埋下的推广参数
if(!typeof(paramObj['uid']) == "undefined"){
document.getElementById('uid').value = paramObj['uid'];
}
var img = new Image(0, 0);
var baseUrl = "http://ad.xxx.com";
if(type == 2){//二跳
if(!this.getCookie(cookieName2)){
if(typeof(paramObj['uid']) == "undefined"){
return false;
}
setCookie(cookieName2, 1, 10, '/', domain, false);
var clickUrl = baseUrl + '/index/one/' + paramObj['uid'] + '/' + Math.random();
img.src = clickUrl;
}
}else{//页面打开
if(!this.getCookie(cookieName)){
if(typeof(paramObj['uid']) == "undefined"){
return false;
}
setCookie(cookieName, 1, 10, '/', domain, false);
var clickUrl = baseUrl + '/index/second/' + paramObj['uid'] + '/' + Math.random();
img.src = clickUrl;
}
}
},
stat = function(){
//页面加载后第一次调用
clickFunc(1);
//添加二跳监听器
var links = document.getElementsByTagName("a");
var inputs = document.getElementsByTagName("input");
var areas = document.getElementsByTagName("area");
var imgs = document.getElementsByTagName("img");
var index = links.length;
for(var i = 0; i < inputs.length; i++){
if(inputs[i].type == 'button' || inputs[i].type == 'submit' || inputs[i].type == 'image'){
links[index++] = inputs[i];
}
}
for(i = 0; i < areas.length; i++){
links[index++] = areas[i];
}
for(i = 0; i < imgs.length; i++){
links[index++] = imgs[i];
}
for(i = 0; i < index; i++){
if(window.addEventListener){
links[i].addEventListener('click', secondClicks, false);
}else{
links[i].attachEvent('onclick', secondClicks);
}
}
}()
}();
广告页连接如下:
http://ad.xxx.com/testad.html?uid=3009