Skip to content

Commit

Permalink
Two methods for converting goals now
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Jan 3, 2020
1 parent 12340ee commit fdd9b88
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ import (

func main() {
api, _ := tonicpow.NewClient(os.Getenv("TONICPOW_API_KEY"), tonicpow.LiveEnvironment, nil)
_ = api.ConvertGoal("new-lead-goal", "s358wef983283...", "", "")
_ = api.ConvertGoalWithSession(123, "visitor-tncpw-session-guid", "from: my blog")
}
```

Expand Down
2 changes: 1 addition & 1 deletion definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const (
fieldShortCode = "short_code"
fieldToken = "token"
fieldUserID = "user_id"
fieldVisitorSessionID = "visitor_session_id"
fieldVisitorSessionGUID = "tncpw_session"

// Model names (used for request endpoints)
modelAdvertiser = "advertisers"
Expand Down
2 changes: 1 addition & 1 deletion examples/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func main() {
// Example: Create a user
//
user := &tonicpow.User{
Email: fmt.Sprintf("Testing%[email protected]", rand.Intn(100000)),
Email: fmt.Sprintf("Tes_ti-ng+%[email protected]", rand.Intn(100000)),
Password: testPassword,
}
if user, err = TonicPowAPI.CreateUser(user); err != nil {
Expand Down
52 changes: 44 additions & 8 deletions goals.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,25 +100,61 @@ func (c *Client) UpdateGoal(goal *Goal, userSessionToken string) (updatedGoal *G
return
}

// ConvertGoal will fire a conversion for a given goal, if successful it will make a new Conversion
// ConvertGoalWithVisitorSession will fire a conversion for a given goal, if successful it will make a new Conversion
//
// For more information: https://docs.tonicpow.com/#caeffdd5-eaad-4fc8-ac01-8288b50e8e27
func (c *Client) ConvertGoal(goalName, visitorSessionID, additionalData, customUserID string) (conversion *Conversion, err error) {
func (c *Client) ConvertGoalWithVisitorSession(goalID uint64, tncpwSession, additionalData string) (conversion *Conversion, err error) {

// Must have a name
if len(goalName) == 0 {
err = fmt.Errorf("missing field: %s", fieldName)
if goalID == 0 {
err = fmt.Errorf("missing field: %s", fieldID)
return
}

// Must have a session guid
if len(tncpwSession) == 0 {
err = fmt.Errorf("missing field: %s", fieldVisitorSessionGUID)
return
}

// Start the post data
data := map[string]string{fieldID: fmt.Sprintf("%d", goalID), fieldVisitorSessionGUID: tncpwSession, fieldAdditionalData: additionalData}

// Fire the request
var response string
if response, err = c.request(fmt.Sprintf("%s/convert", modelGoal), http.MethodPost, data, ""); err != nil {
return
}

// Only a 201 is treated as a success
if err = c.error(http.StatusCreated, response); err != nil {
return
}

// Convert model response
err = json.Unmarshal([]byte(response), &conversion)
return
}

// ConvertGoalWithUserID will fire a conversion for a given goal, if successful it will make a new Conversion
//
// For more information: https://docs.tonicpow.com/#d724f762-329e-473d-bdc4-aebc19dd9ea8
func (c *Client) ConvertGoalWithUserID(goalID uint64, userID uint64, additionalData string) (conversion *Conversion, err error) {

// Must have a name
if goalID == 0 {
err = fmt.Errorf("missing field: %s", fieldID)
return
}

// Must have a session id
if len(visitorSessionID) == 0 {
err = fmt.Errorf("missing field: %s", fieldVisitorSessionID)
// Must have a user id
if userID == 0 {
err = fmt.Errorf("missing field: %s", fieldUserID)
return
}

// Start the post data
data := map[string]string{fieldName: goalName, fieldVisitorSessionID: visitorSessionID, fieldAdditionalData: additionalData, fieldUserID: customUserID}
data := map[string]string{fieldID: fmt.Sprintf("%d", goalID), fieldUserID: fmt.Sprintf("%d", userID), fieldAdditionalData: additionalData}

// Fire the request
var response string
Expand Down

0 comments on commit fdd9b88

Please sign in to comment.