package ga

import (
	galog "github.com/tuyou/galog"
)

const (
	PROJECT_ID = "20659"
	CLIENT_ID  = "Android_5.00_tyGuest,facebook.googleplay.0-hall20659.googleplay.Meowment"
)

var glogger *galog.GALogger

func init() {
	glog, err := galog.NewGALogger("logs", PROJECT_ID, CLIENT_ID, galog.LogTypeTrack)
	if err != nil {
		panic(err)
	}
	glogger = glog
}

func GAlogEvent(event string, userID string, deviceID string, properties map[string]interface{}) {
	newProperties := make(map[string]interface{})
	for k, v := range properties {
		// // JSON encode the value
		// jsonBytes, err := json.Marshal(v)
		// if err != nil {
		// 	newProperties["proj_"+k] = fmt.Sprintf("%v", v) // 无法解析直接转为字符串
		// 	continue
		// }
		// newProperties["proj_"+k] = string(jsonBytes)
		newProperties["proj_"+k] = v
	}
	properties = newProperties
	glogger.
		GetEntry(event).
		SetDeviceID(deviceID).
		SetUserID(userID).
		SetProperties(properties).
		Flush()
}
