Skip to content

Commit

Permalink
Release 2.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
dengshiwei committed Nov 10, 2021
1 parent 75d3916 commit ba597f1
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 4 deletions.
1 change: 1 addition & 0 deletions .idea/sa-sdk-go.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright 2015-2021 Sensors Data Inc.
Copyright 2015-2019 Sensors Data Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
32 changes: 30 additions & 2 deletions sensorsanalytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,15 @@ const (
ITEM_SET = "item_set"
ITEM_DELETE = "item_delete"

SDK_VERSION = "2.0.3"
SDK_VERSION = "2.0.4"
LIB_NAME = "Golang"

MAX_ID_LEN = 255
)

// 静态公共属性
var superProperties map[string]interface{}

type SensorsAnalytics struct {
C consumers.Consumer
ProjectName string
Expand Down Expand Up @@ -116,6 +119,10 @@ func (sa *SensorsAnalytics) Track(distinctId, event string, properties map[strin
nproperties = utils.DeepCopy(properties)
}

// merge super properties
if superProperties != nil {
utils.MergeSuperProperty(superProperties, nproperties)
}
nproperties["$lib"] = LIB_NAME
nproperties["$lib_version"] = SDK_VERSION

Expand All @@ -132,7 +139,10 @@ func (sa *SensorsAnalytics) TrackSignup(distinctId, originId string) error {
}

properties := make(map[string]interface{})

// merge super properties
if superProperties != nil {
utils.MergeSuperProperty(superProperties, properties)
}
properties["$lib"] = LIB_NAME
properties["$lib_version"] = SDK_VERSION

Expand Down Expand Up @@ -250,6 +260,24 @@ func (sa *SensorsAnalytics) ItemDelete(itemType string, itemId string) error {
return sa.C.ItemSend(itemData)
}

// 注册公共属性
func (sa *SensorsAnalytics) RegisterSuperProperties(superProperty map[string]interface{}) {
if superProperties == nil {
superProperties = make(map[string]interface{})
}
utils.MergeSuperProperty(superProperty, superProperties)
}

// 清除公共属性
func (sa *SensorsAnalytics) ClearSuperProperties() {
superProperties = make(map[string]interface{})
}

// 清除指定 key 的公共属性
func (sa *SensorsAnalytics) UnregisterSuperProperty(key string) {
delete(superProperties, key)
}

func getLibProperties() structs.LibProperties {
lp := structs.LibProperties{}
lp.Lib = LIB_NAME
Expand Down
4 changes: 4 additions & 0 deletions structs/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ func (e *EventData) NormalizeData() error {
if len(k) > KEY_MAX {
return errors.New("the max length of property key is 256")
}

if len(k) == 0 {
return errors.New("The key is empty or null.")
}
isMatch := checkPattern([]byte(k))
if !isMatch {
return errors.New("property key must be a valid variable name.")
Expand Down
15 changes: 15 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,21 @@ func NowMs() int64 {
return time.Now().UnixNano() / 1000000
}

// 合并公共属性
func MergeSuperProperty(superProperty map[string]interface{}, properties map[string]interface{}) map[string]interface{} {
if superProperty == nil {
return properties
}

for key, value := range superProperty {
_, ok := properties[key]
if !ok {
properties[key] = value
}
}
return properties
}

func DeepCopy(value map[string]interface{}) map[string]interface{} {
ncopy := deepCopy(value)
if nmap, ok := ncopy.(map[string]interface{}); ok {
Expand Down

0 comments on commit ba597f1

Please sign in to comment.