← back to Designer Wallcoverings

mailers/cc-api/cc-stats.js

33 lines

'use strict';
// Read-only: (1) confirm the campaign's real status (sent vs draft),
// (2) find it in the email_summary list and print stats. Reuses getAccessToken.
const cc = require('./cc-client');
const ID = 'd772eb69-b552-4b4b-9d9b-72a302eceb60';
const g = (u, at) => fetch(u, { headers: { Authorization: 'Bearer ' + at, Accept: 'application/json' } });
(async () => {
  const at = await cc.getAccessToken();

  // 1) status of the activity
  const a = await cc.getCampaignActivity(ID);
  console.log('current_status  :', a.current_status);
  console.log('contact_list_ids:', JSON.stringify(a.contact_list_ids || []));
  console.log('segment_ids     :', JSON.stringify(a.segment_ids || []));

  // 2) find it in the summary list (list endpoint, then match our id)
  const r = await g('https://api.cc.email/v3/reports/summary_reports/email_summary?limit=100', at);
  console.log('\nemail_summary HTTP', r.status);
  if (r.ok) {
    const j = await r.json();
    const rows = j.bulk_email_campaign_summaries || j.results || [];
    const mine = rows.find(x => x.campaign_activity_id === ID);
    if (mine) {
      const c = mine.stats || mine.unique_counts || mine;
      console.log('FOUND — stats:', JSON.stringify(c, null, 2));
    } else {
      console.log(`not in the ${rows.length} recent summaries yet (report not generated — normal right after send).`);
    }
  } else {
    console.log((await r.text()).slice(0, 200));
  }
})().catch(e => { console.error('ERROR:', e.message); process.exitCode = 1; });