Skip to content

Commit

Permalink
Campaign and goal requests
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Dec 30, 2019
1 parent 2066d5d commit e07d390
Show file tree
Hide file tree
Showing 4 changed files with 244 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ Here as example of session tracking for [goal conversions](front_end.md)
- [x] Authentication
- [x] Users
- [x] Advertiser Profiles
- [ ] Campaigns
- [ ] Goals
- [x] Campaigns
- [x] Goals
- [ ] Links

## Examples
Expand Down
55 changes: 55 additions & 0 deletions examples/examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,61 @@ let apiKey = process.env.TONICPOW_API_KEY || ''
advertiser = await TonicPow.updateAdvertiserProfile(advertiser)
console.log('updated name: '+advertiser.name)

//
// Example: Create a campaign
//
let newCampaign = {
advertiser_profile_id: advertiser.id,
currency: 'usd',
description: 'Earn BSV for sharing things you like.',
image_url: 'https://i.imgur.com/TbRFiaR.png',
pay_per_click_rate: 0.01,
target_url: 'https://offers.tonicpow.com',
title: 'TonicPow Offers',
}
let campaign = await TonicPow.createCampaign(newCampaign)
console.log('campaign created', campaign)

//
// Example: Get a campaign
//
campaign = await TonicPow.getCampaign(campaign.id)
console.log('campaign found: '+campaign.title)

//
// Example: Update a campaign
//
campaign.title = 'TonicPow Offers Campaign'
campaign = await TonicPow.updateCampaign(campaign)
console.log('updated title: '+campaign.title)

//
// Example: Create a goal
//
let newGoal = {
campaign_id: campaign.id,
description: 'Bring leads and get paid!',
name: 'new-lead-landing-page',
payout_rate: 0.50,
payout_type: 'flat',
title: 'Landing Page Leads'
}
let goal = await TonicPow.createGoal(newGoal)
console.log('goal created', goal)

//
// Example: Get a goal
//
goal = await TonicPow.getGoal(goal.id)
console.log('goal found: '+goal.title)

//
// Example: Update a goal
//
goal.title = 'Landing Page Leads Goal'
goal = await TonicPow.updateGoal(goal)
console.log('updated title: '+goal.title)


} catch(e){
console.error(e)
Expand Down
186 changes: 186 additions & 0 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,80 @@ async function updateAdvertiserProfile(t, profile, userSessionToken) {
return axios.put(t.config.apiUrl + version + '/advertisers', profile, getOptions(userSessionToken))
}

//
// TonicPow API - Campaign Requests
// =====================================================================================================================
//

// createCampaign will make a new campaign
// Use the userSessionToken if making request on behalf of another user
//
// For more information: https://docs.tonicpow.com/#b67e92bf-a481-44f6-a31d-26e6e0c521b1
async function createCampaign(t, campaign, userSessionToken) {
return axios.post(t.config.apiUrl + version + '/campaigns', campaign, getOptions(userSessionToken))
}

// getCampaign will get an existing campaign
// This will return an error if the campaign is not found (404)
// Use the userSessionToken if making request on behalf of another user
//
// For more information: https://docs.tonicpow.com/#b827446b-be34-4678-b347-33c4f63dbf9e
async function getCampaign(t, campaignId, userSessionToken) {
return axios.get(t.config.apiUrl + version + '/campaigns/details/'+campaignId, getOptions(userSessionToken))
}

// getCampaignBalance will update the models's balance from the chain
//
// For more information: https://docs.tonicpow.com/#b6c60c63-8ac5-4c74-a4a2-cf3e858e5a8d
async function getCampaignBalance(t, campaignId) {
return axios.get(t.config.apiUrl + version + '/campaigns/balance/'+campaignId, getOptions())
}

// updateCampaign will update an existing campaign
// Use the userSessionToken if making request on behalf of another user
//
// For more information: https://docs.tonicpow.com/#665eefd6-da42-4ca9-853c-fd8ca1bf66b2
async function updateCampaign(t, campaign, userSessionToken) {
return axios.put(t.config.apiUrl + version + '/campaigns', campaign, getOptions(userSessionToken))
}

//
// TonicPow API - Goal Requests
// =====================================================================================================================
//

// createGoal will make a new goal
// Use the userSessionToken if making request on behalf of another user
//
// For more information: https://docs.tonicpow.com/#29a93e9b-9726-474c-b25e-92586200a803
async function createGoal(t, goal, userSessionToken) {
return axios.post(t.config.apiUrl + version + '/goals', goal, getOptions(userSessionToken))
}

// getGoal will get an existing goal
// This will return an error if the goal is not found (404)
// Use the userSessionToken if making request on behalf of another user
//
// For more information: https://docs.tonicpow.com/#48d7bbc8-5d7b-4078-87b7-25f545c3deaf
async function getGoal(t, goalId, userSessionToken) {
return axios.get(t.config.apiUrl + version + '/goals/details/'+goalId, getOptions(userSessionToken))
}

// updateGoal will update an existing goal
// Use the userSessionToken if making request on behalf of another user
//
// For more information: https://docs.tonicpow.com/#395f5b7d-6a5d-49c8-b1ae-abf7f90b42a2
async function updateGoal(t, goal, userSessionToken) {
return axios.put(t.config.apiUrl + version + '/goals', goal, getOptions(userSessionToken))
}

// convertGoal 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
async function convertGoal(t, goalName, visitorSessionId, additionalData, customUserId) {
let data = {name: goalName, visitor_session_id: visitorSessionId, additional_data: additionalData, user_id: customUserId}
return axios.post(t.config.apiUrl + version + '/goals/convert', data, getOptions())
}

//
// Export TonicPow JS
Expand Down Expand Up @@ -634,4 +708,116 @@ module.exports = {
}
})
},
createCampaign: async function(campaign, userSessionToken){
return new Promise(async(resolve, reject) => {
try {
blockBrowser(this.initRequired())
let response = await createCampaign(this, campaign, userSessionToken)
resolve(response.data)
} catch(e){
if (typeof e.response !== 'undefined') {
reject(e.response.data)
}
reject(e.message)
}
})
},
getCampaign: async function(campaignId, userSessionToken){
return new Promise(async(resolve, reject) => {
try {
blockBrowser(this.initRequired())
let response = await getCampaign(this, campaignId, userSessionToken)
resolve(response.data)
} catch(e){
if (typeof e.response !== 'undefined') {
reject(e.response.data)
}
reject(e.message)
}
})
},
getCampaignBalance: async function(campaignId){
return new Promise(async(resolve, reject) => {
try {
blockBrowser(this.initRequired())
let response = await getCampaignBalance(this, campaignId)
resolve(response.data)
} catch(e){
if (typeof e.response !== 'undefined') {
reject(e.response.data)
}
reject(e.message)
}
})
},
updateCampaign: async function(campaign, userSessionToken){
return new Promise(async(resolve, reject) => {
try {
blockBrowser(this.initRequired())
let response = await updateCampaign(this, campaign, userSessionToken)
resolve(response.data)
} catch(e){
if (typeof e.response !== 'undefined') {
reject(e.response.data)
}
reject(e.message)
}
})
},
createGoal: async function(goal, userSessionToken){
return new Promise(async(resolve, reject) => {
try {
blockBrowser(this.initRequired())
let response = await createGoal(this, goal, userSessionToken)
resolve(response.data)
} catch(e){
if (typeof e.response !== 'undefined') {
reject(e.response.data)
}
reject(e.message)
}
})
},
getGoal: async function(goalId, userSessionToken){
return new Promise(async(resolve, reject) => {
try {
blockBrowser(this.initRequired())
let response = await getGoal(this, goalId, userSessionToken)
resolve(response.data)
} catch(e){
if (typeof e.response !== 'undefined') {
reject(e.response.data)
}
reject(e.message)
}
})
},
updateGoal: async function(goal, userSessionToken){
return new Promise(async(resolve, reject) => {
try {
blockBrowser(this.initRequired())
let response = await updateGoal(this, goal, userSessionToken)
resolve(response.data)
} catch(e){
if (typeof e.response !== 'undefined') {
reject(e.response.data)
}
reject(e.message)
}
})
},
convertGoal: async function(goalName, visitorSessionId, additionalData, customUserId){
return new Promise(async(resolve, reject) => {
try {
blockBrowser(this.initRequired())
let response = await convertGoal(this, goalName, visitorSessionId, additionalData, customUserId)
resolve(response.data)
} catch(e){
if (typeof e.response !== 'undefined') {
reject(e.response.data)
}
reject(e.message)
}
})
},
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tonicpow-js",
"version": "0.1.8",
"version": "0.1.9",
"description": "TonicPow API Library in JS - https://docs.tonicpow.com",
"main": "lib/api.js",
"repository": {
Expand Down

0 comments on commit e07d390

Please sign in to comment.