diff --git a/README.md b/README.md index ec8b9da..65f5d92 100644 --- a/README.md +++ b/README.md @@ -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") } ``` diff --git a/definitions.go b/definitions.go index 04215d3..e0d7735 100644 --- a/definitions.go +++ b/definitions.go @@ -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" diff --git a/examples/examples.go b/examples/examples.go index 031c775..d322b53 100644 --- a/examples/examples.go +++ b/examples/examples.go @@ -63,7 +63,7 @@ func main() { // Example: Create a user // user := &tonicpow.User{ - Email: fmt.Sprintf("Testing%d@TonicPow.com", rand.Intn(100000)), + Email: fmt.Sprintf("Tes_ti-ng+%d@TonicPow.com", rand.Intn(100000)), Password: testPassword, } if user, err = TonicPowAPI.CreateUser(user); err != nil { diff --git a/goals.go b/goals.go index f2d90f9..39fadc3 100644 --- a/goals.go +++ b/goals.go @@ -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