-
-
Notifications
You must be signed in to change notification settings - Fork 4
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
Showing
5 changed files
with
195 additions
and
113 deletions.
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
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,143 @@ | ||
package tonicpow | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
// CreateConversionByGoalID will fire a conversion for a given goal id, if successful it will make a new Conversion | ||
// | ||
// For more information: https://docs.tonicpow.com/#caeffdd5-eaad-4fc8-ac01-8288b50e8e27 | ||
func (c *Client) CreateConversionByGoalID(goalID uint64, tncpwSession, additionalData string, delayInMinutes int64) (conversion *Conversion, err error) { | ||
|
||
// Must have a name | ||
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{fieldGoalID: fmt.Sprintf("%d", goalID), fieldVisitorSessionGUID: tncpwSession, fieldAdditionalData: additionalData, fieldDelayInMinutes: fmt.Sprintf("%d", delayInMinutes)} | ||
|
||
// Fire the request | ||
var response string | ||
if response, err = c.request(modelConversion, 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 | ||
} | ||
|
||
// CreateConversionByGoalName will fire a conversion for a given goal name, if successful it will make a new Conversion | ||
// | ||
// For more information: https://docs.tonicpow.com/#d19c9850-3832-45b2-b880-3ef2f3b7dc37 | ||
func (c *Client) CreateConversionByGoalName(goalName, tncpwSession, additionalData string, delayInMinutes int64) (conversion *Conversion, err error) { | ||
|
||
// Must have a name | ||
if len(goalName) == 0 { | ||
err = fmt.Errorf("missing field: %s", fieldName) | ||
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{fieldName: goalName, fieldVisitorSessionGUID: tncpwSession, fieldAdditionalData: additionalData, fieldDelayInMinutes: fmt.Sprintf("%d", delayInMinutes)} | ||
|
||
// Fire the request | ||
var response string | ||
if response, err = c.request(modelConversion, 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 | ||
} | ||
|
||
// CreateConversionByUserID will fire a conversion for a given goal and user id, if successful it will make a new Conversion | ||
// | ||
// For more information: https://docs.tonicpow.com/#d724f762-329e-473d-bdc4-aebc19dd9ea8 | ||
func (c *Client) CreateConversionByUserID(goalID, userID uint64, additionalData string, delayInMinutes int64) (conversion *Conversion, err error) { | ||
|
||
// Must have a name | ||
if goalID == 0 { | ||
err = fmt.Errorf("missing field: %s", fieldID) | ||
return | ||
} | ||
|
||
// Must have a user id | ||
if userID == 0 { | ||
err = fmt.Errorf("missing field: %s", fieldUserID) | ||
return | ||
} | ||
|
||
// Start the post data | ||
data := map[string]string{fieldGoalID: fmt.Sprintf("%d", goalID), fieldUserID: fmt.Sprintf("%d", userID), fieldAdditionalData: additionalData, fieldDelayInMinutes: fmt.Sprintf("%d", delayInMinutes)} | ||
|
||
// Fire the request | ||
var response string | ||
if response, err = c.request(modelConversion, 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 | ||
} | ||
|
||
// GetConversion will get an existing conversion | ||
// This will return an error if the goal is not found (404) | ||
// | ||
// For more information: https://docs.tonicpow.com/#fce465a1-d8d5-442d-be22-95169170167e | ||
func (c *Client) GetConversion(conversionID uint64) (conversion *Conversion, err error) { | ||
|
||
// Must have an id | ||
if conversionID == 0 { | ||
err = fmt.Errorf("missing field: %s", fieldID) | ||
return | ||
} | ||
|
||
// Fire the request | ||
var response string | ||
if response, err = c.request(fmt.Sprintf("%s/details/%d", modelConversion, conversionID), http.MethodGet, nil, ""); err != nil { | ||
return | ||
} | ||
|
||
// Only a 200 is treated as a success | ||
if err = c.error(http.StatusOK, response); err != nil { | ||
return | ||
} | ||
|
||
// Convert model response | ||
err = json.Unmarshal([]byte(response), &conversion) | ||
return | ||
} |
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
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
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