创业项目APP开发必须快速迭代和快速试错,而试错根据什么来呢?
1.用户评价,用户调研,用户回访;
2.数据分析,用户行为跟踪;
而用户调研对提问的要求很大,你获得的都是你提问的回复,也就是你看到的,特别是用户量不大的情况下,更是难上加难,调研比较分散如何参考?
但是用户行为深入分析会获得用户背后的想法,因此初期也很有必要。下面直接切入正题,结合代码我们看看如何快速生成用户行为跟踪。
session one: 记录访问api日志
访问服务器的API通过nginx代理,而nginx log记录重点信息,其中nginx.conf日志配置如下(直接上代码,下同):
log_format main '$remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_user_agent" '
'"$http_x_forwarded_for" "$request_time" "$upstream_response_time"';
location ~ /api/ {
access_log /var/log/nginx/api.log main;
//记录日志示例如下,参照看即可知每个字段的含义:
117.175.2.33 - - [24/Oct/2018:06:40:46 +0800] POST /api/first HTTP/1.1 "200" 496 "HiBabyPsy/2.2.3 (iPhone; iOS 12.0; Scale/2.00)" "-" "0.007" "0.007"
117.136.75.147 - - [24/Oct/2018:06:25:22 +0800] POST /api/version HTTP/1.1 "200" 319 "okhttp/3.6.0" "-" "0.007" "0.007"
session two: 解析日志生成行为excel(python代码)
可以用jenkins执行定时任务,每天凌晨左右执行python脚本:
第一步,下载前一天的日志api.log.1进行分析
import paramiko
t =paramiko.Transport((HOST,22))
t.connect(username=USER,password=PASSWD)
sftp =paramiko.SFTPClient.from_transport(t)
logfile ="api.log.%s" % getoffsetday(-1)
sftp.get('/var/log/nginx/api.log.1', logfile)
t.close()
第二步,解析api为行为名称,存入excel
API= {"/api/version":"查询版本",
"/api/first":"首页",...}
def analysis_log(logfile):
clients = {} # client = {ip: [{timestamp:, url:, response:,}],...} system:iOS/Android
print("===start analysis_log====")
with open(logfile,'rb')as f:
for linein f:
s = line.split()
ip = s[0]
timestamp = time.mktime(time.strptime(s[3][1:],'%d/%b/%Y:%H:%M:%S'))
url = s[6]
if 'iOS' in sor 'iPhone' in s:
system ='iOS'
else:
system ='Android'
response = s[-1]
c = system +'-' + ip
if c not in clients:
clients[c] = [dict(url=url,timestamp=timestamp,response=response)]
else:
clients[c][-1]['timestamp'] = timestamp - clients[c][-1]['timestamp']
clients[c].append(dict(url=url,timestamp=timestamp,response=response))
data = xlwt.Workbook('log_%s.xlsx' % getoffsetday(-1))
format = data.add_format({'bold':True})
format.set_font_name('SimSun')# 指定“宋体”
format.set_font_color('blue')
table= data.add_worksheet('sheet1')
head = [u"序号",u"手机用户",u'行为',u"停留时长",u"响应时间"]
for colin range(5):
table.write(0, col, head[col], format)
i =1
for k, vin clients.items():
for uin v:
url = u['url'].split('?')[0]
if urlin API:
action = API.get(url)
else:
action = url
if not action:
continue
if u['timestamp'] >200:
second =200
else:
second = u['timestamp']
row = [i, k, unicode(action), second, u['response']]
for colin range(5):
table.write(i, col, row[col])
i +=1
data.close()
print("===end analysis_log====")
生成excel行为记录如下:
session three: 图表分析行为
利用透视表和各种图形展示用户行为:
到此结束,有了行为日志,数据分析就看功底了。
网友评论