Xây dựng một hệ thống tracking hành vi người dùng (phần 1)



 Hiện nay trên các trang thương mại điện tử việc tracking hành vi người dùng rất là phổ biến vậy nên hôm nay mình sẽ viết một bài viết về hệ thống tracking đơn giản đã từng được sử dụng trên một website thương mại điện tử nhé.Ngôn ngữ mình sử dụng trong bài viết này là ruby(ruby on rails)
1.Sơ đồ tổng quan hệ thống
Ứng với sơ đồ tổng quan sẽ có 2 data set tương ứng như sau
2.Triển khai project
Mô tả sơ qua về cách xây dựng hệ thống(bỏ qua các giai đoạn xây dựng xác thực tài khoản và phân quyền,vì chỉ là thành phần phụ trợ).
Tạo tag -> lưu tag vào database -> render thành file js với đầu vào là các tag được tạo từ  trước
2.1.Các model tương ứng

Tag model
 class Tag  
  include Mongoid::Document  
  include Mongoid::Timestamps  
  belongs_to :user  
  field :name, type: String, default: ""  
  field :selector, type: Hash, default: {}  
  field :trigger, type: Hash, default: {}  
  field :handle, type: Hash, default: {}  
  field :author, type: String, default: ""  
 end 

User model
 class User  
  include Mongoid::Document  
  has_many :tags  
  # Include default devise modules. Others available are:  
  # :confirmable, :lockable, :timeoutable and :omniauthable  
  devise :database_authenticatable, :registerable,  
      :recoverable, :rememberable, :trackable, :validatable  
  ## Database authenticatable  
  ## admin?  
  field :admin, type: Boolean, default: false  
  ## approval  
  field :approval, type: Boolean, default: false  
  field :email,       type: String, default: ""  
  field :encrypted_password, type: String, default: ""  
  ## Recoverable  
  field :reset_password_token,  type: String  
  field :reset_password_sent_at, type: Time  
  ## Rememberable  
  field :remember_created_at, type: Time  
  ## Trackable  
  field :sign_in_count,   type: Integer, default: 0  
  field :current_sign_in_at, type: Time  
  field :last_sign_in_at,  type: Time  
  field :current_sign_in_ip, type: String  
  field :last_sign_in_ip,  type: String  
  ## Confirmable  
  # field :confirmation_token,  type: String  
  # field :confirmed_at,     type: Time  
  # field :confirmation_sent_at, type: Time  
  # field :unconfirmed_email,  type: String # Only if using reconfirmable  
  ## Lockable  
  # field :failed_attempts, type: Integer, default: 0 # Only if lock strategy is :failed_attempts  
  # field :unlock_token,  type: String # Only if unlock strategy is :email or :both  
  # field :locked_at,    type: Time  
  validates :email, uniqueness: true  
  def email_required?  
   false  
  end  
  def email_changed?  
   false  
  end  
  def will_save_change_to_email?  
   false  
  end  
  def admin?  
   self.admin  
  end  
 end  

Sau khi có được model tiếp theo tiến hành viết controller thực hiện việc các thao tác với  tag (thêm,sửa,xóa..)
Tag Controller
 module Tagmanager  
  class HomeController < ApplicationController  
   skip_before_action :verify_authenticity_token  
   layout 'tagmanager_v1'  
   before_action :authenticate_user!  
   def index  
    # response.headers.delete "X-Frame-Options"  
    if current_user.approval  
     render template: "tagmanager/home"  
    else  
     render template: "index"  
    end  
   end  
   def create  
    if params.blank?  
     flash[:notice] = "Create tag fail,because data tag send from client empty"  
     redirect_to root_url  
    end  
    t = Tag.new  
    if !current_user.blank?  
     t.user_id = current_user.id  
     t.author = current_user.email  
    else  
     t.user_id = "anonymous_user"  
    end  
    t.name = params["tag_name"]  
    t.selector["dom"] = params["selector"].downcase  
    t.selector["compare"] = params["compare"].downcase  
    t.selector["value"] = params["selector-value"]  
    t.selector["other_value"] = params["selector-other-value"]  
    t.trigger["value"] = params["trigger"].downcase  
    if !params["other-trigger"].blank?  
     t.trigger["other_value"] = params["other-trigger"]  
    else  
     t.trigger.delete "other_value"  
    end  
    if !params["other-handle"].blank?  
     t.handle["other_handle"] = params["other-handle"]  
    else  
     t.handle.delete "other_handle"  
    end  
    t.handle["value"] = params["handle"].downcase  
    if !t.name.blank? && !t.selector["value"].blank?  
     t.save  
     return redirect_to root_url + "tagmanager/list"  
    end  
     redirect_to root_url  
   end  
   def show  
    # @tags = Tag.order_by(:user_id => "desc")  
    current_id = current_user[:id]  
    my_tag = Tag.where(:user_id => current_id).to_a  
    other_tag = Tag.not_in(:user_id => current_id).to_a  
    @tags = my_tag + other_tag  
    render template: 'tagmanager/list_tags'  
   end  
   def update_tag  
    id = params[:id]  
    @tag_detail = Tag.where(:id => id).first  
    authorize! :update, @tag_detail  
    render template: 'tagmanager/edit_tag_detail'  
   end  
   def save_edit_tag  
    tag_id = params[:tag_id]  
    data_tag =Tag.where(:id => tag_id).first  
    if !params["other-trigger"].blank?  
     data_tag.trigger["other_value"] = params["other-trigger"]  
    else  
     data_tag.trigger.delete "other_value"  
    end  
    data_tag.name = params["tag_name"]  
    data_tag.selector["dom"] = params["selector"]  
    data_tag.selector["compare"] = params["compare"]  
    data_tag.selector["value"] = params["selector-value"]  
    data_tag.selector["other_value"] = params["selector-other-value"]  
    data_tag.trigger["value"] = params["trigger"]  
    data_tag.handle["value"] = params["handle"]  
    data_tag.save  
    redirect_to root_url + "tagmanager/list"  
   end  
   def destroy  
    tag_id = params[:tag_id]  
    @tag_destroy = Tag.find(:id => tag_id)  
    authorize! :destroy, @tag_destroy  
    @tag_destroy.destroy  
   end  
   def view_tag  
    id = params[:id]  
    @tag_view = Tag.find(:id => id)  
    render template: "tagmanager/view_tag_detail"  
   end  
  end  

Nhận xét

  1. Siêu thị Bếp Hoàng Cương - TP HCM
    Địa chỉ: 348 Bạch Đằng - Q Bình Thạnh - TP HCM
    Tel: 0974329191
    Website: https://bephoangcuong.com

    Siêu thị Bếp Hoàng Cương - Hà Nội
    Địa chỉ: 398 Khâm Thiên - quận Đống Đa - Hà Nội
    Tel: 0933266966
    Youtube: Clip Bếp Hoàng Cương
    Facebook: Facebook Fanpage Bếp Hoàng Cương

    Trả lờiXóa

Đăng nhận xét

Bài đăng phổ biến từ blog này

Cài đặt SSL cho website sử dụng certbot

Xây dựng một hệ thống comment real-time hoặc chat đơn giản sử dụng Pusher

CÁC BÀI TẬP SQL CƠ BẢN - PART 1

Xây dựng một hệ thống tracking hành vi người dùng (phần 2)

Enterprise architecture trên 1 tờ A4

Web caching (P2)

Bàn về async/await trong vòng lặp javascript

Web caching (P1)

Cài đặt môi trường để code website Rails