-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Guan Wei
committed
Apr 18, 2018
1 parent
f0a0d1b
commit 045cd1f
Showing
27 changed files
with
1,984 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Copyright 2015 SensorsData Inc. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,47 @@ | ||
# sa-sdk-go | ||
# Sensors Analytics | ||
|
||
This is the official Golang SDK for Sensors Analytics. | ||
|
||
## Easy Installation | ||
|
||
You can get Sensors Analytics SDK using go. | ||
|
||
``` | ||
go get github.com/sensorsdata/sa-sdk-go | ||
``` | ||
Or update sdk with | ||
``` | ||
go get -u github.com/sensorsdata/sa-sdk-go | ||
``` | ||
|
||
Once the SDK is successfully installed, use the Sensors Analytics SDK likes: | ||
|
||
```golang | ||
import sdk "github.com/sensorsdata/sa-sdk-go" | ||
|
||
// Gets the url of Sensors Analytics in the home page. | ||
SA_SERVER_URL = 'YOUR_SERVER_URL' | ||
|
||
// Initialized the Sensors Analytics SDK with Default Consumer | ||
consumer = sdk.InitDefaultConsumer(SA_SERVER_URL) | ||
sa = sdk.InitSensorsAnalytics(consumer) | ||
|
||
properties := map[string]interface{}{ | ||
"price": 12, | ||
"name": "apple", | ||
"somedata": []string{"a", "b"}, | ||
} | ||
|
||
// Track the event 'ServerStart' | ||
sa.track("ABCDEFG1234567", "ServerStart", properties, false) | ||
|
||
sa.Close() | ||
``` | ||
|
||
## More Examples | ||
([Examples](_examples)) | ||
|
||
## To Learn More | ||
See our [full manual](http://www.sensorsdata.cn/manual/golang_sdk.html) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
===================== | ||
Sensors Analytics SDK | ||
===================== | ||
|
||
Sensors Analytics SDK | ||
===================== | ||
|
||
This is the official Golang SDK for Sensors Analytics. | ||
|
||
To Learn More | ||
------------- | ||
|
||
See our `full manual <http://www.sensorsdata.cn/manual/golang_sdk.html>`_. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package main | ||
import ( | ||
"fmt" | ||
sdk "github.com/sensorsdata/sa-sdk-go" | ||
) | ||
|
||
func main() { | ||
c, err := sdk.InitBatchConsumer("http://localhost:8106/sa?project=production", 3, 1000) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
sa := sdk.InitSensorsAnalytics(c, "default", false) | ||
defer sa.Close() | ||
|
||
distinctId := "ABCDEF123456" | ||
event := "ViewProduct" | ||
properties := map[string]interface{}{ | ||
"$ip": "2.2.2.2", | ||
"ProductId": "123456", | ||
"ProductCatalog": "Laptop Computer", | ||
"IsAddedToFav": true, | ||
} | ||
|
||
err = sa.Track(distinctId, event, properties, true) | ||
if err != nil { | ||
fmt.Println("track failed", err) | ||
return | ||
} | ||
|
||
err = sa.Track(distinctId, event, properties, true) | ||
if err != nil { | ||
fmt.Println("track failed", err) | ||
return | ||
} | ||
|
||
err = sa.Track(distinctId, event, properties, true) | ||
if err != nil { | ||
fmt.Println("track failed", err) | ||
return | ||
} | ||
|
||
fmt.Println("track done") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package main | ||
import ( | ||
"fmt" | ||
sdk "github.com/sensorsdata/sa-sdk-go" | ||
) | ||
|
||
func main() { | ||
c, err := sdk.InitConcurrentLoggingConsumer("./log.data", false) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
sa := sdk.InitSensorsAnalytics(c, "default", false) | ||
defer sa.Close() | ||
|
||
distinctId := "ABCDEF123456" | ||
event := "ViewProduct" | ||
properties := map[string]interface{}{ | ||
"$ip": "2.2.2.2", | ||
"ProductId": "123456", | ||
"ProductCatalog": "Laptop Computer", | ||
"IsAddedToFav": true, | ||
} | ||
|
||
err = sa.Track(distinctId, event, properties, true) | ||
if err != nil { | ||
fmt.Println("track failed", err) | ||
return | ||
} | ||
|
||
fmt.Println("track done") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package main | ||
import ( | ||
"fmt" | ||
sdk "github.com/sensorsdata/sa-sdk-go" | ||
) | ||
|
||
func main() { | ||
c, err := sdk.InitDebugConsumer("http://localhost:8106/sa?project=production", false, 1000) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
sa := sdk.InitSensorsAnalytics(c, "default", false) | ||
|
||
distinctId := "ABCDEF123456777" | ||
event := "ViewInfo" | ||
properties := map[string]interface{}{ | ||
"$ip": "2.2.2.2", | ||
"ProductId": "123456", | ||
"ProductCatalog": "Laptop Computer", | ||
"IsAddedToFav": true, | ||
} | ||
|
||
err = sa.Track(distinctId, event, properties, false) | ||
if err != nil { | ||
fmt.Println("track failed", err) | ||
return | ||
} | ||
|
||
fmt.Println("track done") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package main | ||
import ( | ||
"fmt" | ||
sdk "github.com/sensorsdata/sa-sdk-go" | ||
) | ||
|
||
func main() { | ||
c, err := sdk.InitDefaultConsumer("http://localhost:8106/sa", 1000) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
sa := sdk.InitSensorsAnalytics(c, "default", false) | ||
defer sa.Close() | ||
|
||
distinctId := "12345" | ||
event := "ViewProduct" | ||
properties := map[string]interface{}{ | ||
"price": 12, | ||
"name": "apple", | ||
"somedata": []string{"a", "b"}, | ||
} | ||
|
||
err = sa.Track(distinctId, event, properties, true) | ||
if err != nil { | ||
fmt.Println("track failed", err) | ||
return | ||
} | ||
|
||
fmt.Println("track done") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
sdk "github.com/sensorsdata/sa-sdk-go" | ||
) | ||
|
||
func main() { | ||
c, err := sdk.InitLoggingConsumer("./log.data", false) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
sa := sdk.InitSensorsAnalytics(c, "default", false) | ||
defer sa.Close() | ||
|
||
distinctId := "ABCDEF123456" | ||
event := "ViewProduct" | ||
properties := map[string]interface{}{ | ||
"$ip": "2.2.2.2", | ||
"ProductId": "123456", | ||
"ProductCatalog": "Laptop Computer", | ||
"IsAddedToFav": true, | ||
} | ||
|
||
err = sa.Track(distinctId, event, properties, true) | ||
if err != nil { | ||
fmt.Println("track failed", err) | ||
return | ||
} | ||
|
||
fmt.Println("track done") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package consumers | ||
|
||
import ( | ||
"time" | ||
"encoding/json" | ||
|
||
"github.com/sensorsdata/sa-sdk-go/structs" | ||
) | ||
|
||
const ( | ||
BATCH_DEFAULT_MAX = 50 | ||
) | ||
|
||
|
||
type BatchConsumer struct { | ||
Url string | ||
Max int | ||
buffer []structs.EventData | ||
Timeout time.Duration | ||
} | ||
|
||
func InitBatchConsumer(url string, max, timeout int) (*BatchConsumer, error) { | ||
if max > BATCH_DEFAULT_MAX { | ||
max = BATCH_DEFAULT_MAX | ||
} | ||
|
||
c := &BatchConsumer{Url: url, Max: max, Timeout: time.Duration(timeout) * time.Millisecond} | ||
c.buffer = make([]structs.EventData, 0, max) | ||
|
||
return c, nil | ||
} | ||
|
||
func (c *BatchConsumer) Send(data structs.EventData) error { | ||
c.buffer = append(c.buffer, data) | ||
if len(c.buffer) >= c.Max { | ||
return c.Flush() | ||
} | ||
return nil | ||
} | ||
|
||
func (c *BatchConsumer) Flush() error { | ||
jdata, err := json.Marshal(c.buffer) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
send(c.Url, string(jdata), c.Timeout, true) | ||
|
||
c.buffer = c.buffer[:0] | ||
|
||
return nil | ||
} | ||
|
||
func (c *BatchConsumer) Close() error { | ||
return c.Flush() | ||
} |
Oops, something went wrong.