GithubHelp home page GithubHelp logo

clevertap-node's Introduction

clevertap-node

npm version

CI Status

Fully async Node.js server module for accessing the CleverTap Server API

Install

npm install clevertap

Usage

// require the library
const CleverTap = require('clevertap');
/**
 init the library with your CleverTap Account Id, CleverTap Account Passcode and CleverTap Account Region
 Clevertap Account Regions:
 EUROPE: 'eu1', // default for most accounts
 INDIA: 'in1',
 SINGAPORE: 'sg1',
 US: 'us1',
 INDONESIA: 'aps3',
 MIDDLEEAST: 'mec1'
*/
const clevertap = CleverTap.init(YOUR_CLEVERTAP_ACCOUNT_ID, YOUR_CLEVERTAP_ACCOUNT_PASSCODE, CleverTap.REGIONS.EUROPE);

// the library supports both callbacks and Promises

/* Upload Profile or events */
var data = [
  {
    type: "event",
    identity: "6264372124",
    evtName: "choseNewFavoriteFood",
    evtData: {
      value: "sushi",
    },
  },

  {
    type: "profile",
    identity: "6264372124",
    ts: t,
    profileData: {
      favoriteColor: "green",
      Age: 30,
      Phone: "+14155551234",
      Email: "[email protected]",
    },
  },

  {
    type: "event",
    FBID: "34322423",
    evtName: "Product viewed",
    evtData: {
      "Product name": "Casio Chronograph Watch",
      Category: "Mens Watch",
      Price: 59.99,
      Currency: "USD",
    },
  },

  {
    type: "profile",
    objectId: "-2ce3cca260664f70b82b1c6bb505f462",
    profileData: { favoriteFood: "hot dogs" },
  },

  {
    type: "event",
    objectId: "-2ce3cca260664f70b82b1c6bb505f462",
    evtName: "choseNewFavoriteFood",
    evtData: {},
  },

  {
    type: "event",
    identity: "[email protected]",
    ts: t,
    evtName: "Charged",
    evtData: {
      Amount: 300,
      Currency: "USD",
      "Payment mode": "Credit Card",
      Items: [
        {
          Category: "books",
          "Book name": "The millionaire next door",
          Quantity: 1,
        },
        {
          Category: "books",
          "Book name": "Achieving inner zen",
          Quantity: 4,
        },
      ],
    },
  },
];
// callback style
clevertap.upload(data, { debug: 1, batchSize: 1000 }, (res) => {
  console.log(res);
});
// or if you prefer Promises
clevertap.upload(data, { debug: 1, batchSize: 1000 }).then((res) => {
  console.log(res);
});

/* Get Events data */
var eventsQuery = {
  event_name: "choseNewFavoriteFood",
  props: [{ name: "value", operator: "contains", value: "piz" }],
  from: 20150810,
  to: 20151025,
};
// callback style
clevertap.events(eventsQuery, { debug: 1, batchSize: 6000 }, (res) => {
  console.log(res);
});
// or if you prefer Promises
clevertap.events(eventsQuery, { debug: 1, batchSize: 6000 }).then((res) => {
  console.log(res);
});

/* Get profile info */
//callback style
clevertap.profile(
  { objectId: "-2ce3cca260664f70b82b1c6bb505f462", debug: 1 },
  (res) => {
    console.log(res);
  }
);
// or if you prefer Promises
clevertap
  .profile({ objectId: "-2ce3cca260664f70b82b1c6bb505f462", debug: 1 })
  .then((res) => {
    console.log(res);
  });

var profilesQuery = {
  event_name: "choseNewFavoriteFood",
  props: [{ name: "value", operator: "contains", value: "piz" }],
  from: 20150810,
  to: 20151025,
};
//callback style
clevertap.profiles(profilesQuery, { debug: 1, batchSize: 200 }, (res) => {
  console.log(res);
});
// or if you prefer Promises
clevertap.profiles(profilesQuery, { debug: 1, batchSize: 200 }).then((res) => {
  console.log(res);
});

/* Campaign API's - CREATE, ESTIMATE, LIST, RESULT, STOP, TARGET_ACTIONS */
var estimatePayload = {
  name: "green freedom",
  when: "now",
  where: {
    event_name: "App Launched",
    from: 20160101,
    to: 20160317,
  },
  content: {
    title: "Hello!",
    body: "Strictly Green Lantern fans only!",
    platform_specific: {
      ios: {
        deep_link: "judepereira.com",
        sound_file: "judepereira.wav",
        category: "reactive",
        badge_count: 1,
        foo: "bar_ios",
      },
      android: {
        background_image: "http://judepereira.com/a.jpg",
        default_sound: true,
        deep_link: "judepereira.com",
        foo: "bar_android",
      },
    },
  },
  devices: ["android", "ios"],
};
//callback style
clevertap.targets(
  clevertap.TARGET_ESTIMATE,
  estimatePayload,
  { debug: 1 },
  (res) => {
    console.log(res);
  }
);
// or if you prefer Promises
clevertap
  .targets(clevertap.TARGET_ESTIMATE, estimatePayload, { debug: 1 })
  .then((res) => {
    console.log(res);
  });

var createPayload = {
  name: "green freedom",
  when: "now",
  where: {
    event_name: "App Launched",
    from: 20160101,
    to: 20160317,
  },
  content: {
    title: "Hello!",
    body: "Strictly Green Lantern fans only!",
    platform_specific: {
      ios: {
        deep_link: "judepereira.com",
        sound_file: "judepereira.wav",
        category: "reactive",
        badge_count: 1,
        foo: "bar_ios",
      },
      android: {
        background_image: "http://judepereira.com/a.jpg",
        default_sound: true,
        deep_link: "judepereira.com",
        foo: "bar_android",
      },
    },
  },
  devices: ["ios"],
};
//callback style
clevertap.targets(
  clevertap.TARGET_CREATE,
  createPayload,
  { debug: 1 },
  (res) => {
    console.log(res);
  }
);
// or if you prefer Promises
clevertap
  .targets(clevertap.TARGET_CREATE, createPayload, { debug: 1 })
  .then((res) => { console.log(res); });

var listPayload = { from: 20160101, to: 20160317 };
//callback style
clevertap.targets(clevertap.TARGET_LIST, listPayload, { debug: 1 }, (res) => { console.log(res); });
// or if you prefer Promises
clevertap
  .targets(clevertap.TARGET_LIST, listPayload, { debug: 1 })
  .then((res) => {console.log(res);});

var stopPayload = { id: 1458261857 };
//callback style
clevertap.targets(clevertap.TARGET_STOP, stopPayload, { debug: 1 }, (res) => {console.log(res);});
// or if you prefer Promises
clevertap
  .targets(clevertap.TARGET_STOP, stopPayload, { debug: 1 })
  .then((res) => { console.log(res);});

var resultPayload = { id: 1458261857 };
//callback style
clevertap.targets(
  clevertap.TARGET_RESULT,
  resultPayload,
  { debug: 1 },
  (res) => {console.log(res);}
);
// or if you prefer Promises
clevertap
  .targets(clevertap.TARGET_RESULT, resultPayload, { debug: 1 })
  .then((res) => {console.log(res);});

/* Report API's */
// Get Message Reports
var getMessageReportspayload = { from: "20171101", to: "20171225" };
clevertap
  .getMessageReports(getMessageReportspayload, { debug: 1 })
  .then((res) => {console.log(res);});

// This endpoint enables you to get a real-time count of active users in the past five minutes in your app
var realTimeCountspayload = { user_type: true };
clevertap.realTimeCounts(realTimeCountspayload, { debug: 1 }).then((res) => {console.log(res);});

// This endpoint is used to retrieve counts for the most and least frequently occurring properties for a particular event in a specified duration
var topPropertyCountspayload = {
  event_name: "Charged",
  from: 20161229,
  to: 20170129,
  groups: {
    foo: {
      property_type: "event_properties",
      name: "Amount",
    },
    bar: {
      property_type: "profile_fields",
      name: "Customer Type",
      top_n: 2,
      order: "asc",
    },
  },
};
clevertap
  .topPropertyCounts(topPropertyCountspayload, { debug: 1 })
  .then((res) => {console.log(res);});

// This endpoint is used to retrieve daily, weekly, and monthly event trends in a specified duration.
var trendspayload = {
  event_name: "Charged",
  from: 20161224,
  to: 20170103,
  unique: false,
  sum_event_prop: "Amount",
  groups: {
    foo: {
      trend_type: "daily",
    },
    bar: {
      trend_type: "weekly",
    },
    foobar: {
      trend_type: "monthly",
    },
  },
};
clevertap.trends(trendspayload, { debug: 1 }).then((res) => {console.log(res);});

See example.js for more detailed usage.

Also please see our Server API documentation.

Tests

npm install  
npm test // all tests 

clevertap-node's People

Contributors

abhishek-g-clevertap avatar droyson avatar kamblesonam avatar prashantpandey10 avatar pwilkniss avatar rangakoushik avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

clevertap-node's Issues

#DEPRECATED REQUEST PACKAGE

A quick question, please what about those packages that use this request module how do we bypass the deprecation warnings cause this message always popup up when I am installing firebase across devices.
npm WARN deprecated [email protected]: request has been deprecated, see https://github.com/request/request/issues/3142

ReferenceError: HTMLElement is not defined

If I don't use .tsx I keep getting this error. It only works alright if it is in .tsx. Error happens if the file is in .ts, .jsx, .js

Server Error
ReferenceError: HTMLElement is not defined

This error happened while generating the page. Any console logs will be displayed in the terminal window.
Call Stack
<unknown>
file:///C:/Users/ASUS/Desktop/Self-Study/NEXT_REACT/clevertap-next-integration/node_modules/.pnpm/[email protected]/node_modules/clevertap-web-sdk/clevertap.js (3474:36)
<unknown>
file:///C:/Users/ASUS/Desktop/Self-Study/NEXT_REACT/clevertap-next-integration/node_modules/.pnpm/[email protected]/node_modules/clevertap-web-sdk/clevertap.js (2:83)
Object.<anonymous>
file:///C:/Users/ASUS/Desktop/Self-Study/NEXT_REACT/clevertap-next-integration/node_modules/.pnpm/[email protected]/node_modules/clevertap-web-sdk/clevertap.js (5:2)
Module._compile
node:internal/modules/cjs/loader (1256:14)
Module._extensions..js
node:internal/modules/cjs/loader (1310:10)
Module.load
node:internal/modules/cjs/loader (1119:32)
Module._load
node:internal/modules/cjs/loader (960:12)
Module.require
node:internal/modules/cjs/loader (1143:19)
mod.require
file:///C:/Users/ASUS/Desktop/Self-Study/NEXT_REACT/clevertap-next-integration/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/server/require-hook.js (65:28)
require
node:internal/modules/cjs/helpers (121:18)
clevertap-web-sdk/clevertap
file:///C:/Users/ASUS/Desktop/Self-Study/NEXT_REACT/clevertap-next-integration/.next/server/pages/index.js (87:18)
__webpack_require__
file:///C:/Users/ASUS/Desktop/Self-Study/NEXT_REACT/clevertap-next-integration/.next/server/webpack-runtime.js (33:42)
eval
webpack-internal:///./pages/index.js (9:85)
./pages/index.js
file:///C:/Users/ASUS/Desktop/Self-Study/NEXT_REACT/clevertap-next-integration/.next/server/pages/index.js (55:1)
__webpack_require__
file:///C:/Users/ASUS/Desktop/Self-Study/NEXT_REACT/clevertap-next-integration/.next/server/webpack-runtime.js (33:42)

in my package.json:

"dependencies": {
    "clevertap": "^1.3.0",
    "clevertap-web-sdk": "^1.6.9",
    "next": "14.0.4",
"react": "^18",
    "react-dom": "^18"

index.js

import { useState, useEffect } from "react";
import CleverTap from "clevertap-web-sdk/clevertap";

const profileApiUrl = "/profile";
export default function Home() {
  const [clevertapModule, setClevertapModule] = useState(null);

  useEffect(() => {
    clevertapInit();
  }, []);

  const clevertapInit = async () => {
    let clevertap = clevertapModule;
    if (!clevertap) {
      clevertap = await initializeClevertap();
    }

    if (clevertap) {
      clevertap.event.push("Amee"); // Popup Campaign
      clevertap.event.push("webpopup test"); // Banner Campaign
      clevertap.event.push("InternalTest2"); // Inbox Campaign
    }
  };

  const profileData = {
    d: [
      {
        identity: "345543",
        type: "profile",
        profileData: {
          Name: "John Doe",
          Email: "[email protected]",
        },
      },
    ],
  };


    fetch("/clevertap/upload", {
      method: "POST",
      headers: {
        "X-CleverTap-Account-Id": "******",
        "X-CleverTap-Passcode": "*******",
        "Content-Type": "application/json",
        // Add any other required headers here
      },
      body: JSON.stringify(profileData),
    })
      .then((response) => {
        if (!response.ok) {
          throw new Error("Network response was not ok");
        }
        return response.json();
      })
      .then((data) => {
        // Handle the API response data here
        console.log(data);
      })
      .catch((error) => {
        // Handle any errors that occurred during the API call
        console.error("Error:", error);
      });
  

  return (
    <div>
      {/* Navigation Bar */}
      <nav style={{ backgroundColor: "#333", padding: "10px", color: "#fff" }}>
        <ul
          style={{
            listStyle: "none",
            display: "flex",
            justifyContent: "space-between",
            maxWidth: "300px",
          }}
        >
          <li>Home</li>
          <li>About</li>
          <li>Contact</li>
          <li id="inbox">Inbox</li>
        </ul>
      </nav>

      {/* Hero Section */}
      <section
        style={{
          backgroundColor: "#f2f2f2",
          padding: "140px 0",
          textAlign: "center",
        }}
      >
        <div>
          <h1 style={{ fontSize: "36px" }}>Welcome to Our Website</h1>
          <p style={{ fontSize: "20px", color: "#666" }}>
            We provide amazing services for you.
          </p>
          <button
            style={{
              padding: "10px 20px",
              fontSize: "16px",
              background: "#007bff",
              color: "#fff",
              border: "none",
              borderRadius: "5px",
              cursor: "pointer",
            }}
          >
            Get Started
          </button>
        </div>
      </section>

      {/* Clevertap Banner / carousel */}
      <div id="heroDiv"></div>

      {/* Call-to-action Section */}
      <section
        style={{
          backgroundColor: "#007bff",
          padding: "60px 0",
          textAlign: "center",
          color: "#fff",
        }}
      >
        <h2 style={{ fontSize: "30px" }}>Sign Up Now</h2>
        <p style={{ fontSize: "18px" }}>
          Join us and experience the best services.
        </p>
        <button
          style={{
            padding: "10px 20px",
            fontSize: "16px",
            background: "#fff",
            color: "#007bff",
            border: "none",
            borderRadius: "5px",
            cursor: "pointer",
          }}
        >
          Sign Up
        </button>
      </section>
    </div>
  );
}

async function initializeClevertap() {
  const clevertapModule = await import("clevertap-web-sdk");

  clevertapModule.default.init("*******");
  clevertapModule.default.privacy.push({ optOut: false });
  clevertapModule.default.privacy.push({ useIP: false });
  clevertapModule.default.setLogLevel(3);

  return clevertapModule.default;
}

Can't install with yarn on recent node versions

Hello, when can we expect a new version published in NPM?

It's been a while since the problem was fixed but no release was published since:

#1

yarn add clevertap
yarn add v1.17.3
[1/4] ๐Ÿ” Resolving packages...
[2/4] ๐Ÿšš Fetching packages...
error [email protected]: The engine "node" is incompatible with this module. Expected version "4.2.3". Got "12.10.0"
error Found incompatible module.

Can you publish a new version ASAP?

Invalid JavaScript

Hi! ๐Ÿ‘‹

Firstly, thanks for your work on this project! ๐Ÿ™‚

Today I used patch-package to patch [email protected] for the project I'm working on.

Details

There's a missing variable declaration (or a misplaced semicolon, should be a comma). This fails in more recent JS versions since it's not actual valid JS.

Here is the diff that solved my problem:

diff --git a/node_modules/clevertap/lib/clevertap-api.js b/node_modules/clevertap/lib/clevertap-api.js
index 4d16bfd..48d5794 100644
--- a/node_modules/clevertap/lib/clevertap-api.js
+++ b/node_modules/clevertap/lib/clevertap-api.js
@@ -3,7 +3,7 @@ const request       = require('request');
 const querystring   = require('querystring');
 
 const API_HOSTNAME  = "api.clevertap.com";
-      API_VERSION   = 1;
+const API_VERSION   = 1;
 
 const CREATE = "create",
       ESTIMATE = "estimate",

This issue body was partially generated by patch-package.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.