Created
November 23, 2023 14:18
-
-
Save robert-moore/4dc2f989b28a47b0c9b66f9ac1f9e88a to your computer and use it in GitHub Desktop.
Churnkey Event Creation
This file contains hidden or 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
const Axios = require("axios"); | |
const churnkeyEventUrl = "https://api.churnkey.co/v1/api/events/new"; | |
const churnkeyBulkEventUrl = "https://api.churnkey.co/v1/api/events/bulk"; | |
const churnkeyCustomerUpdateUrl = | |
"https://api.churnkey.co/v1/api/events/customer-update"; | |
const churnkeyAppId = process.env['CK_APP_ID'] | |
const churnkeySecretKey = process.env['CK_SECRET_KEY'] | |
if (!churnkeyAppId || !churnkeySecretKey) { | |
console.log('Missing Churnkey App ID or Secret Key'); | |
} | |
const headers = { | |
"x-ck-app": churnkeyAppId, | |
"x-ck-api-key": churnkeySecretKey, | |
"content-type": "application/json", | |
}; | |
async function createEvent() { | |
const res = await Axios.post( | |
churnkeyEventUrl, | |
{ | |
uid: "your_internal_user_id", // e.g. DB id | |
customerId: "billing_provider_id", // e.g. from Stripe, Chargebee, etc. | |
event: "Video Published", | |
eventData: { | |
platform: "facebook", // optional | |
}, | |
}, | |
{ headers } | |
); | |
return res.data; | |
} | |
async function customerUpdate() { | |
const res = await Axios.post( | |
churnkeyCustomerUpdateUrl, | |
{ | |
uid: "your_internal_user_id", // e.g. DB id | |
customerId: "billing_provider_id", // e.g. from Stripe, Chargebee, etc. | |
customerData: { | |
name: "Jane Doe", | |
videosPublished: 5, | |
}, | |
}, | |
{ headers } | |
); | |
return res.data; | |
} | |
async function backfillData() { | |
const backfillEvents = [ | |
{ | |
uid: "user_abc", | |
createdAt: "2023-05-01", | |
event: "Video Published", | |
}, | |
{ | |
uid: "user_xyz", | |
createdAt: "2023-05-02", | |
event: "Video Downloaded", | |
}, | |
]; | |
const res = await Axios.post(churnkeyBulkEventUrl, backfillEvents, { | |
headers, | |
}); | |
return res.data; | |
} | |
async function main() { | |
console.log('Running...') | |
const eventResult = await createEvent(); | |
console.log(eventResult); | |
const updateResult = await customerUpdate(); | |
console.log(updateResult); | |
const backfillResult = await backfillData(); | |
console.log(backfillResult); | |
process.exit() | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment