Skip to content

Commit

Permalink
Optional options.utmCampaign for iCalendar tracking URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
mjradwin committed Aug 1, 2021
1 parent f2e90e3 commit 323ff49
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 32 deletions.
22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hebcal/icalendar",
"version": "4.11.0",
"version": "4.12.0",
"author": "Michael J. Radwin (https://github.com/mjradwin)",
"keywords": [
"ical",
Expand All @@ -24,8 +24,8 @@
"url": "https://github.com/hebcal/hebcal-icalendar/issues"
},
"dependencies": {
"@hebcal/core": "^3.21.0",
"@hebcal/rest-api": "^3.7.0"
"@hebcal/core": "^3.23.0",
"@hebcal/rest-api": "^3.8.0"
},
"scripts": {
"build": "rollup -c",
Expand Down Expand Up @@ -54,21 +54,21 @@
"verbose": true
},
"devDependencies": {
"@ava/babel": "^1.0.1",
"@babel/core": "^7.14.6",
"@ava/babel": "^2.0.0",
"@babel/core": "^7.14.8",
"@babel/polyfill": "^7.12.1",
"@babel/preset-env": "^7.14.7",
"@babel/preset-env": "^7.14.8",
"@babel/register": "^7.14.5",
"@rollup/plugin-babel": "^5.3.0",
"@rollup/plugin-commonjs": "^19.0.0",
"@rollup/plugin-commonjs": "^20.0.0",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^13.0.0",
"@rollup/plugin-node-resolve": "^13.0.4",
"ava": "^3.15.0",
"eslint": "^7.30.0",
"eslint": "^7.32.0",
"eslint-config-google": "^0.14.0",
"jsdoc": "^3.6.7",
"jsdoc-to-markdown": "^6.0.1",
"jsdoc-to-markdown": "^7.0.1",
"murmurhash-js": "^1.0.0",
"rollup": "^2.53.0"
"rollup": "^2.55.1"
}
}
23 changes: 15 additions & 8 deletions src/icalendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,18 @@ function addOptional(arr, key, val) {
/**
* @private
* @param {string} url
* @param {boolean} il
* @param {HebrewCalendar.Options} options
* @return {string}
*/
function appendTrackingToUrl(url, il) {
return url ? appendIsraelAndTracking(url, il, 'js', 'icalendar') : url;
function appendTrackingToUrl(url, options) {
if (!url) {
return null;
}
const utmSource = options.utmSource || 'js';
const utmMedium = options.utmMedium || 'icalendar';
const utmCampaign = options.utmCampaign;
return appendIsraelAndTracking(url,
options.il, utmSource, utmMedium, utmCampaign);
}

const char74re = /(.{1,74})/g;
Expand Down Expand Up @@ -171,7 +178,7 @@ export class IcalEvent {

const options = this.options;
// create memo (holiday descr, Torah, etc)
const memo = createMemo(ev, options.il);
const memo = createMemo(ev, options);
addOptional(arr, 'DESCRIPTION', memo);
addOptional(arr, 'LOCATION', this.locationName);
if (this.timed && options.location) {
Expand Down Expand Up @@ -271,10 +278,10 @@ export function eventToIcal(ev, options) {
/**
* @private
* @param {Event} e
* @param {boolean} il
* @param {HebrewCalendar.Options} options
* @return {string}
*/
function createMemo(e, il) {
function createMemo(e, options) {
const desc = e.getDesc();
const candles = (desc === 'Havdalah' || desc === 'Candle lighting');
if (candles) {
Expand All @@ -284,8 +291,8 @@ function createMemo(e, il) {
if (mask & flags.OMER_COUNT) {
return e.getTodayIs('en') + '\\n\\n' + e.memo;
}
const url = appendTrackingToUrl(e.url(), il);
const torahMemo = makeTorahMemoText(e, il).replace(/\n/g, '\\n');
const url = appendTrackingToUrl(e.url(), options);
const torahMemo = makeTorahMemoText(e, options.il).replace(/\n/g, '\\n');
if (mask & flags.PARSHA_HASHAVUA) {
return torahMemo + '\\n\\n' + url;
} else {
Expand Down
62 changes: 49 additions & 13 deletions src/icalendar.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,9 @@ test('ical-sedra', (t) => {
'X-MICROSOFT-CDO-BUSYSTATUS:FREE',
'X-MICROSOFT-CDO-ALLDAYEVENT:TRUE',
'CLASS:PUBLIC',
'DESCRIPTION:Torah: Numbers 16:1-18:32\\nMaftir: Numbers 28:9 - 28:15 | Shab',
' bat Rosh Chodesh\\nHaftarah: Isaiah 66:1 - 66:24 | Shabbat Rosh Chodesh\\n\\n',
' https://www.hebcal.com/sedrot/korach-19930619?utm_source=js&utm_medium=ica',
' lendar',
'DESCRIPTION:Torah: Numbers 16:1-18:32\\, 28:9-15\\nHaftarah: Isaiah 66:1 - 6',
' 6:24 | Shabbat Rosh Chodesh\\n\\nhttps://www.hebcal.com/sedrot/korach-199306',
' 19?utm_source=js&utm_medium=icalendar',
'END:VEVENT',
];
t.deepEqual(lines, expected);
Expand Down Expand Up @@ -125,9 +124,9 @@ test('ical-transp-opaque', (t) => {
'X-MICROSOFT-CDO-ALLDAYEVENT:TRUE',
'CLASS:PUBLIC',
'DESCRIPTION:Passover\\, the Feast of Unleavened Bread\\n\\nTorah: Leviticus 2',
' 2:26-23:44\\; Numbers 28:16-28:25\\nHaftarah: II Kings 23:1 - 23:9\\; 23:21 -',
' 23:25\\n\\nhttps://www.hebcal.com/holidays/pesach-1993?utm_source=js&utm_me',
' dium=icalendar',
' 2:26-23:44\\; Numbers 28:16-25\\nHaftarah: II Kings 23:1 - 23:9\\; 23:21 - 23',
' :25\\n\\nhttps://www.hebcal.com/holidays/pesach-1993?utm_source=js&utm_mediu',
' m=icalendar',
'END:VEVENT',
];
t.deepEqual(lines, expected);
Expand Down Expand Up @@ -177,7 +176,7 @@ test('ical-candles', (t) => {
lines = havdalah.toString().split('\r\n');
t.is(lines.length, 13);
t.is(lines[0], 'BEGIN:VEVENT');
t.is(findLine(lines, 'SUMMARY'), '🌃 Havdalah');
t.is(findLine(lines, 'SUMMARY'), ' Havdalah');
t.is(findLine(lines, 'LOCATION'), 'Chicago');
});

Expand Down Expand Up @@ -287,14 +286,14 @@ test('appendHebrewToSubject', (t) => {
const summary = icals.map((i) => i.toString().split('\r\n').find((s) => s.startsWith('SUMMARY')));
const expected = [
'SUMMARY:Parashat Bamidbar / פרשת בְּמִדְבַּר',
'SUMMARY:🌃 Havdalah / הַבדָלָה',
'SUMMARY: Havdalah / הַבדָלָה',
'SUMMARY:🌒 Rosh Chodesh Sivan / רֹאשׁ חוֹדֶשׁ סִיוָן',
'SUMMARY:⛰️🌸 Erev Shavuot / עֶרֶב שָׁבוּעוֹת',
'SUMMARY:🕯️ Candle lighting / הַדלָקָת נֵרוֹת',
'SUMMARY:⛰️🌸 Shavuot I / שָׁבוּעוֹת יוֹם א׳',
'SUMMARY:🕯️ Candle lighting / הַדלָקָת נֵרוֹת',
'SUMMARY:⛰️🌸 Shavuot II / שָׁבוּעוֹת יוֹם ב׳',
'SUMMARY:🌃 Havdalah / הַבדָלָה',
'SUMMARY: Havdalah / הַבדָלָה',
];
t.deepEqual(summary, expected);
});
Expand Down Expand Up @@ -357,9 +356,10 @@ test('ical-il-url', (t) => {
'X-MICROSOFT-CDO-BUSYSTATUS:OOF',
'X-MICROSOFT-CDO-ALLDAYEVENT:TRUE',
'CLASS:PUBLIC',
'DESCRIPTION:Eighth Day of Assembly\\n\\nTorah: Deuteronomy 33:1-34:12\\; Numb',
' ers 29:35-30:1\\nHaftarah: Joshua 1:1 - 1:18\\n\\nhttps://www.hebcal.com/holi',
' days/shmini-atzeret-2021?i=on&utm_source=js&utm_medium=icalendar',
'DESCRIPTION:Eighth Day of Assembly\\n\\nTorah: Deuteronomy 33:1-34:12\\; Gene',
' sis 1:1-2:3\\; Numbers 29:35-30:1\\nHaftarah: Joshua 1:1 - 1:18\\n\\nhttps://w',
' ww.hebcal.com/holidays/shmini-atzeret-2021?i=on&utm_source=js&utm_medium=i',
' calendar',
'END:VEVENT',
];
t.deepEqual(lines, expected);
Expand Down Expand Up @@ -497,3 +497,39 @@ test('OmerEvent', (t) => {
];
t.deepEqual(lines, expected);
});

/** @private */
class TestEvent extends Event {
/** @param {HDate} date */
constructor(date) {
super(date, 'Test Event', 0);
}
/** @return {string} */
url() {
return 'https://www.hebcal.com/foobar';
}
}

test('utm_campaign', (t) => {
const ev = new TestEvent(new HDate(22, 'Iyyar', 5781));
const icalEvent = new IcalEvent(ev, {utmSource: 'baaz', utmCampaign: 'quux'});
const lines = icalEvent.toString().split('\r\n');
lines[1] = 'DTSTAMP:X';
const expected = [
'BEGIN:VEVENT',
'DTSTAMP:X',
'CATEGORIES:Holiday',
'SUMMARY:Test Event',
'DTSTART;VALUE=DATE:20210504',
'DTEND;VALUE=DATE:20210505',
'UID:hebcal-20210504-9f01ca16',
'TRANSP:TRANSPARENT',
'X-MICROSOFT-CDO-BUSYSTATUS:FREE',
'X-MICROSOFT-CDO-ALLDAYEVENT:TRUE',
'CLASS:PUBLIC',
'DESCRIPTION:https://www.hebcal.com/foobar?utm_source=baaz&utm_medium=icale',
' ndar&utm_campaign=quux',
'END:VEVENT',
];
t.deepEqual(lines, expected);
});

0 comments on commit 323ff49

Please sign in to comment.