|
@@ -1,16 +1,38 @@
|
|
package resourceChannel
|
|
package resourceChannel
|
|
|
|
|
|
import (
|
|
import (
|
|
- "bytes"
|
|
|
|
"encoding/json"
|
|
"encoding/json"
|
|
- "io/ioutil"
|
|
|
|
- "net/http"
|
|
|
|
- "net/url"
|
|
|
|
|
|
+ "github.com/go-redis/redis"
|
|
|
|
+ "golang.org/x/net/context"
|
|
"strconv"
|
|
"strconv"
|
|
"strings"
|
|
"strings"
|
|
"time"
|
|
"time"
|
|
)
|
|
)
|
|
|
|
|
|
|
|
+func redisCli() *redis.Client {
|
|
|
|
+ client := redis.NewClient(&redis.Options{
|
|
|
|
+ Addr: coo.RedisHost, // Redis地址
|
|
|
|
+ Password: "", // Redis密码,默认为空
|
|
|
|
+ DB: 1, // 使用哪个数据库,默认为0
|
|
|
|
+ })
|
|
|
|
+ _, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
|
|
|
|
+ defer cancel()
|
|
|
|
+ return client
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+type ResourceAssessment struct {
|
|
|
|
+ ID uint `gorm:"primaryKey" json:"id"` // 留学评估表
|
|
|
|
+ DataType uint8 `json:"data_type"` // 1.网课 2.api资源 3.专题页 4.工作台录入 5.后台录入 6 小程序
|
|
|
|
+ ChatID string `json:"chat_id"` // 会话id
|
|
|
|
+ Channel int `json:"channel"` // 渠道id
|
|
|
|
+ Category int `json:"category"` // 分类id
|
|
|
|
+ CreatedAt time.Time `json:"created_at"` // 数据创建时间
|
|
|
|
+ ReferPageURL string `json:"refer_page_url"` // 来源网址
|
|
|
|
+ URL string `json:"url"` // 落地页地址
|
|
|
|
+ ChatURL string `json:"chat_url"` // 对话发起页
|
|
|
|
+ GrUserId string `json:"gr_user_id"` // 神策distinct_id
|
|
|
|
+}
|
|
|
|
+
|
|
type SemJsChatRecord struct {
|
|
type SemJsChatRecord struct {
|
|
ChatID int64 `gorm:"column:CHAT_ID"`
|
|
ChatID int64 `gorm:"column:CHAT_ID"`
|
|
CreateTime time.Time `gorm:"column:CREATE_TIME"`
|
|
CreateTime time.Time `gorm:"column:CREATE_TIME"`
|
|
@@ -21,6 +43,11 @@ type SemJsChatRecord struct {
|
|
GrUserID string `gorm:"column:gr_user_id"`
|
|
GrUserID string `gorm:"column:gr_user_id"`
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+type ChannelCategory struct {
|
|
|
|
+ Channel int
|
|
|
|
+ Category int
|
|
|
|
+}
|
|
|
|
+
|
|
// GetChatChannel 会话渠道归因处理
|
|
// GetChatChannel 会话渠道归因处理
|
|
func GetChatChannel(chat SemJsChatRecord) ChannelCategory {
|
|
func GetChatChannel(chat SemJsChatRecord) ChannelCategory {
|
|
channel := 7
|
|
channel := 7
|
|
@@ -38,20 +65,15 @@ func GetChatChannel(chat SemJsChatRecord) ChannelCategory {
|
|
channel = 38
|
|
channel = 38
|
|
category = 201
|
|
category = 201
|
|
} else if chat.ChannleID == 5 || chat.ChannleID == 7 && !strings.Contains(chat.ReferPage, "utm") && !strings.Contains(chat.ChatURL, "utm") {
|
|
} else if chat.ChannleID == 5 || chat.ChannleID == 7 && !strings.Contains(chat.ReferPage, "utm") && !strings.Contains(chat.ChatURL, "utm") {
|
|
- layout := "2006-01-02 15:04:05"
|
|
|
|
- startTime := chat.CreateTime.Add(-1 * time.Hour)
|
|
|
|
- start := startTime.Format(layout)
|
|
|
|
- end := chat.CreateTime.Format(layout)
|
|
|
|
- sql := "select $url from events where distinct_id = '" + chat.GrUserID + "'and event = '$pageview' and time between '" + start + "' and '" + end + "' order by time limit 1"
|
|
|
|
- url := getSensorsData(sql)
|
|
|
|
- if strings.Contains(url, "utm") && strings.Contains(strings.ToLower(url), "link") {
|
|
|
|
- println(url)
|
|
|
|
- println(strings.Split(url, "link")[1])
|
|
|
|
- channelIDString := strings.Split(strings.Split(url, "link")[1], "_")[1]
|
|
|
|
- categoryIDString := strings.Split(strings.Split(url, "link")[1], "_")[2]
|
|
|
|
- channel, _ = strconv.Atoi(channelIDString)
|
|
|
|
- category, _ = strconv.Atoi(categoryIDString)
|
|
|
|
- } else if url == "https://www.jjl.cn/" || url == "https://m.jjl.cn/" {
|
|
|
|
|
|
+ // redis获取渠道信息
|
|
|
|
+ key := "sensors:" + chat.GrUserID
|
|
|
|
+ cc, _ := redisCli().Get(key).Result()
|
|
|
|
+ var sensors SensorsData
|
|
|
|
+ json.Unmarshal([]byte(cc), &sensors)
|
|
|
|
+ if sensors.Channel > 0 {
|
|
|
|
+ channel = sensors.Channel
|
|
|
|
+ category = sensors.Category
|
|
|
|
+ } else if sensors.Url == "https://www.jjl.cn/" || sensors.Url == "https://m.jjl.cn/" {
|
|
channel = 7
|
|
channel = 7
|
|
category = 126
|
|
category = 126
|
|
} else {
|
|
} else {
|
|
@@ -81,25 +103,7 @@ func GetChatChannel(chat SemJsChatRecord) ChannelCategory {
|
|
return channelCategory
|
|
return channelCategory
|
|
}
|
|
}
|
|
|
|
|
|
-type ResourceAssessment struct {
|
|
|
|
- ID uint `gorm:"primaryKey" json:"id"` // 留学评估表
|
|
|
|
- DataType uint8 `json:"data_type"` // 1.网课 2.api资源 3.专题页 4.工作台录入 5.后台录入 6 小程序
|
|
|
|
- ChatID string `json:"chat_id"` // 会话id
|
|
|
|
- Channel int `json:"channel"` // 渠道id
|
|
|
|
- Category int `json:"category"` // 分类id
|
|
|
|
- CreatedAt time.Time `json:"created_at"` // 数据创建时间
|
|
|
|
- ReferPageURL string `json:"refer_page_url"` // 来源网址
|
|
|
|
- URL string `json:"url"` // 落地页地址
|
|
|
|
- ChatURL string `json:"chat_url"` // 对话发起页
|
|
|
|
- GrUserId string `json:"gr_user_id"` // 神策distinct_id
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-type ChannelCategory struct {
|
|
|
|
- Channel int
|
|
|
|
- Category int
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-// GetChannel 渠道归因处理
|
|
|
|
|
|
+// GetChannel 表单渠道归因处理
|
|
func GetChannel(assessment ResourceAssessment) ChannelCategory {
|
|
func GetChannel(assessment ResourceAssessment) ChannelCategory {
|
|
var channel int
|
|
var channel int
|
|
var category int
|
|
var category int
|
|
@@ -126,16 +130,14 @@ func GetChannel(assessment ResourceAssessment) ChannelCategory {
|
|
}
|
|
}
|
|
} else if assessment.DataType == 3 || ((assessment.Channel == 5 || assessment.Channel == 7) && assessment.ChatID != "") {
|
|
} else if assessment.DataType == 3 || ((assessment.Channel == 5 || assessment.Channel == 7) && assessment.ChatID != "") {
|
|
if !strings.Contains(assessment.URL, "utm") && !strings.Contains(assessment.ChatURL, "utm") && !strings.Contains(assessment.ReferPageURL, "utm") {
|
|
if !strings.Contains(assessment.URL, "utm") && !strings.Contains(assessment.ChatURL, "utm") && !strings.Contains(assessment.ReferPageURL, "utm") {
|
|
- layout := "2006-01-02 15:04:05"
|
|
|
|
- startTime := assessment.CreatedAt.Add(-1 * time.Hour)
|
|
|
|
- start := startTime.Format(layout)
|
|
|
|
- end := assessment.CreatedAt.Format(layout)
|
|
|
|
- sql := "select $url from events where distinct_id = '" + assessment.GrUserId + "'and event = '$pageview' and time between '" + start + "' and '" + end + "' order by time limit 1"
|
|
|
|
- url := getSensorsData(sql)
|
|
|
|
- if strings.Contains(url, "utm") && strings.Contains(url, "link") {
|
|
|
|
- channel, _ = strconv.Atoi(strings.Split(strings.Split(strings.Split(url, "link")[1], "form")[0], "_")[1])
|
|
|
|
- category, _ = strconv.Atoi(strings.Split(strings.Split(strings.Split(url, "link")[1], "form")[0], "_")[2])
|
|
|
|
- } else if url == "https://www.jjl.cn/" || url == "https://m.jjl.cn/" {
|
|
|
|
|
|
+ key := "sensors:" + assessment.GrUserId
|
|
|
|
+ cc, _ := redisCli().Get(key).Result()
|
|
|
|
+ var sensors SensorsData
|
|
|
|
+ json.Unmarshal([]byte(cc), &sensors)
|
|
|
|
+ if sensors.Channel > 0 {
|
|
|
|
+ channel = sensors.Channel
|
|
|
|
+ category = sensors.Category
|
|
|
|
+ } else if sensors.Url == "https://www.jjl.cn/" || sensors.Url == "https://m.jjl.cn/" {
|
|
channel = 7
|
|
channel = 7
|
|
category = 126
|
|
category = 126
|
|
} else {
|
|
} else {
|
|
@@ -184,31 +186,9 @@ func GetChannel(assessment ResourceAssessment) ChannelCategory {
|
|
return channelCategory
|
|
return channelCategory
|
|
}
|
|
}
|
|
|
|
|
|
-// Sensors 神策数据结构体
|
|
|
|
-type Sensors struct {
|
|
|
|
- Url string `json:"$url"`
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-// 访问神策API
|
|
|
|
-func getSensorsData(sql string) string {
|
|
|
|
- sensorsUrl := "https://sensorsview.jjl.cn/api/sql/query?token=128d837f466684101710cfb308e7556de975b717c6c2465ba5fc2836d57a64c8&project=production&type=impala&q=" + url.QueryEscape(sql) + "&format=json"
|
|
|
|
- data := []byte("")
|
|
|
|
- req, err := http.NewRequest("POST", sensorsUrl, bytes.NewBuffer(data))
|
|
|
|
- if err != nil {
|
|
|
|
- return err.Error()
|
|
|
|
- }
|
|
|
|
- req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
- client := &http.Client{}
|
|
|
|
- resp, err := client.Do(req)
|
|
|
|
- if err != nil {
|
|
|
|
- return err.Error()
|
|
|
|
- }
|
|
|
|
- defer resp.Body.Close()
|
|
|
|
- respBody, err := ioutil.ReadAll(resp.Body)
|
|
|
|
- if err != nil {
|
|
|
|
- return err.Error()
|
|
|
|
- }
|
|
|
|
- var sensors Sensors
|
|
|
|
- json.Unmarshal([]byte(string(respBody)), &sensors)
|
|
|
|
- return sensors.Url
|
|
|
|
|
|
+type SensorsData struct {
|
|
|
|
+ DistinctId string `json:"distinctID"`
|
|
|
|
+ Channel int `json:"channel"`
|
|
|
|
+ Category int `json:"category"`
|
|
|
|
+ Url string `json:"url"`
|
|
}
|
|
}
|