@sellout/utils 0.0.72 → 0.0.73
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.dist/ClientTrackingUtil.d.ts +9 -0
- package/.dist/ClientTrackingUtil.js +37 -0
- package/.dist/ClientTrackingUtil.js.map +1 -0
- package/.dist/FacebookUtil.d.ts +8 -0
- package/.dist/FacebookUtil.js +77 -0
- package/.dist/FacebookUtil.js.map +1 -0
- package/.dist/GoogleUtil.d.ts +8 -0
- package/.dist/GoogleUtil.js +91 -0
- package/.dist/GoogleUtil.js.map +1 -0
- package/package.json +4 -4
- package/src/ClientTrackingUtil.ts +44 -0
- package/src/FacebookUtil.ts +63 -0
- package/src/GoogleUtil.ts +81 -0
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare enum ClientTrackingEventTypes {
|
|
2
|
+
Purchase = "Purchase"
|
|
3
|
+
}
|
|
4
|
+
export interface IClientTrackingIds {
|
|
5
|
+
googleAnalyticsId?: string;
|
|
6
|
+
facebookPixelId?: string;
|
|
7
|
+
}
|
|
8
|
+
export declare function initialize(clientTrackingIds: IClientTrackingIds): void;
|
|
9
|
+
export declare function track(type: ClientTrackingEventTypes, params: any): void | null;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.track = exports.initialize = exports.ClientTrackingEventTypes = void 0;
|
|
4
|
+
const GoogleUtil = require("./GoogleUtil");
|
|
5
|
+
const FacebookUtil = require("./FacebookUtil");
|
|
6
|
+
var ClientTrackingEventTypes;
|
|
7
|
+
(function (ClientTrackingEventTypes) {
|
|
8
|
+
ClientTrackingEventTypes["Purchase"] = "Purchase";
|
|
9
|
+
})(ClientTrackingEventTypes = exports.ClientTrackingEventTypes || (exports.ClientTrackingEventTypes = {}));
|
|
10
|
+
;
|
|
11
|
+
function initialize(clientTrackingIds) {
|
|
12
|
+
GoogleUtil.initialize(clientTrackingIds.googleAnalyticsId);
|
|
13
|
+
FacebookUtil.initialize(clientTrackingIds.facebookPixelId);
|
|
14
|
+
}
|
|
15
|
+
exports.initialize = initialize;
|
|
16
|
+
function track(type, params) {
|
|
17
|
+
switch (type) {
|
|
18
|
+
case ClientTrackingEventTypes.Purchase:
|
|
19
|
+
return trackPurchase(params);
|
|
20
|
+
default:
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.track = track;
|
|
25
|
+
function trackPurchase({ order, orderTotal, channel, currency = 'USD', }) {
|
|
26
|
+
GoogleUtil.track('event', 'purchase', {
|
|
27
|
+
transaction_id: order._id,
|
|
28
|
+
value: orderTotal / 100,
|
|
29
|
+
affiliation: channel,
|
|
30
|
+
currency,
|
|
31
|
+
});
|
|
32
|
+
FacebookUtil.track('track', 'Purchase', {
|
|
33
|
+
value: orderTotal / 100,
|
|
34
|
+
currency,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=ClientTrackingUtil.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ClientTrackingUtil.js","sourceRoot":"","sources":["../src/ClientTrackingUtil.ts"],"names":[],"mappings":";;;AAAA,2CAA2C;AAC3C,+CAA+C;AAE/C,IAAY,wBAEX;AAFD,WAAY,wBAAwB;IAClC,iDAAqB,CAAA;AACvB,CAAC,EAFW,wBAAwB,GAAxB,gCAAwB,KAAxB,gCAAwB,QAEnC;AAAA,CAAC;AAOF,SAAgB,UAAU,CAAC,iBAAqC;IAC9D,UAAU,CAAC,UAAU,CAAC,iBAAiB,CAAC,iBAAiB,CAAC,CAAC;IAC3D,YAAY,CAAC,UAAU,CAAC,iBAAiB,CAAC,eAAe,CAAC,CAAC;AAC7D,CAAC;AAHD,gCAGC;AAED,SAAgB,KAAK,CAAC,IAA8B,EAAE,MAAW;IAC/D,QAAQ,IAAI,EAAE;QACZ,KAAK,wBAAwB,CAAC,QAAQ;YACpC,OAAO,aAAa,CAAC,MAAM,CAAC,CAAC;QAC/B;YACE,OAAO,IAAI,CAAC;KACf;AACH,CAAC;AAPD,sBAOC;AAED,SAAS,aAAa,CAAC,EACrB,KAAK,EACL,UAAU,EACV,OAAO,EACP,QAAQ,GAAG,KAAK,GACjB;IACC,UAAU,CAAC,KAAK,CAAC,OAAO,EAAE,UAAU,EAAE;QACpC,cAAc,EAAE,KAAK,CAAC,GAAG;QACzB,KAAK,EAAE,UAAU,GAAG,GAAG;QACvB,WAAW,EAAE,OAAO;QACpB,QAAQ;KACT,CAAC,CAAC;IAEH,YAAY,CAAC,KAAK,CAAC,OAAO,EAAE,UAAU,EAAE;QACtC,KAAK,EAAE,UAAU,GAAG,GAAG;QACvB,QAAQ;KACT,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/****************************************************************************************
|
|
2
|
+
Initialization
|
|
3
|
+
****************************************************************************************/
|
|
4
|
+
export declare function initialize(facebookPixelId?: string | null): void;
|
|
5
|
+
/****************************************************************************************
|
|
6
|
+
Tracking
|
|
7
|
+
****************************************************************************************/
|
|
8
|
+
export declare function track(type: string, action: string, params: object): Promise<void>;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.track = exports.initialize = void 0;
|
|
13
|
+
const Wait = require("./wait");
|
|
14
|
+
const SELLOUT_FACEBOOK_PIXEL_ID = '205397967210775';
|
|
15
|
+
function waitForPixel() {
|
|
16
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
17
|
+
return yield Wait.forTrue(() => Boolean(window.fbq));
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
/****************************************************************************************
|
|
21
|
+
Initialization
|
|
22
|
+
****************************************************************************************/
|
|
23
|
+
function initialize(facebookPixelId) {
|
|
24
|
+
try {
|
|
25
|
+
injectPixel(SELLOUT_FACEBOOK_PIXEL_ID);
|
|
26
|
+
if (facebookPixelId) {
|
|
27
|
+
injectPixel(facebookPixelId);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
console.log('Facebook Pixel not configured');
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
catch (e) {
|
|
34
|
+
console.error('Failed to inject Facebook Pixel');
|
|
35
|
+
console.error(e);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
exports.initialize = initialize;
|
|
39
|
+
function injectPixel(facebookPixelId) {
|
|
40
|
+
const headCode = `
|
|
41
|
+
< !--Facebook Pixel Code-- >
|
|
42
|
+
<script>
|
|
43
|
+
!function(f,b,e,v,n,t,s)
|
|
44
|
+
{if(f.fbq)return;n=f.fbq=function(){n.callMethod ?
|
|
45
|
+
n.callMethod.apply(n, arguments) : n.queue.push(arguments)};
|
|
46
|
+
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
|
|
47
|
+
n.queue=[];t=b.createElement(e);t.async=!0;
|
|
48
|
+
t.src=v;s=b.getElementsByTagName(e)[0];
|
|
49
|
+
s.parentNode.insertBefore(t,s)}(window, document,'script',
|
|
50
|
+
'https://connect.facebook.net/en_US/fbevents.js');
|
|
51
|
+
fbq('init', '${facebookPixelId}');
|
|
52
|
+
fbq('track', 'PageView');
|
|
53
|
+
</script>
|
|
54
|
+
<noscript><img height="1" width="1" style="display:none"
|
|
55
|
+
src="https://www.facebook.com/tr?id=${facebookPixelId}&ev=PageView&noscript=1"
|
|
56
|
+
/></noscript>
|
|
57
|
+
<!--End Facebook Pixel Code-- >
|
|
58
|
+
`;
|
|
59
|
+
const headScript = document.createRange().createContextualFragment(headCode);
|
|
60
|
+
document.head.prepend(headScript);
|
|
61
|
+
}
|
|
62
|
+
/****************************************************************************************
|
|
63
|
+
Tracking
|
|
64
|
+
****************************************************************************************/
|
|
65
|
+
function track(type, action, params) {
|
|
66
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
67
|
+
try {
|
|
68
|
+
yield waitForPixel();
|
|
69
|
+
window.fbq(type, action, params);
|
|
70
|
+
}
|
|
71
|
+
catch (e) {
|
|
72
|
+
console.error('Facebook Pixel failed to track event');
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
exports.track = track;
|
|
77
|
+
//# sourceMappingURL=FacebookUtil.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FacebookUtil.js","sourceRoot":"","sources":["../src/FacebookUtil.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,+BAA+B;AAE/B,MAAM,yBAAyB,GAAG,iBAAiB,CAAC;AAEpD,SAAe,YAAY;;QACzB,OAAO,MAAM,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,OAAO,CAAE,MAAc,CAAC,GAAG,CAAC,CAAC,CAAC;IAChE,CAAC;CAAA;AAED;;yFAEyF;AAEzF,SAAgB,UAAU,CAAC,eAA+B;IACxD,IAAI;QACF,WAAW,CAAC,yBAAyB,CAAC,CAAC;QACvC,IAAI,eAAe,EAAE;YACnB,WAAW,CAAC,eAAe,CAAC,CAAC;SAC9B;aAAM;YACL,OAAO,CAAC,GAAG,CAAC,+BAA+B,CAAC,CAAC;SAC9C;KACF;IAAC,OAAO,CAAC,EAAE;QACV,OAAO,CAAC,KAAK,CAAC,iCAAiC,CAAC,CAAC;QACjD,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;KAClB;AACH,CAAC;AAZD,gCAYC;AAED,SAAS,WAAW,CAAC,eAAuB;IAC1C,MAAM,QAAQ,GAAG;;;;;;;;;;;mBAWA,eAAe;;;;4CAIU,eAAe;;;GAGxD,CAAC;IAEF,MAAM,UAAU,GAAG,QAAQ,CAAC,WAAW,EAAE,CAAC,wBAAwB,CAAC,QAAQ,CAAC,CAAC;IAC7E,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;AACpC,CAAC;AAED;;yFAEyF;AAEzF,SAAsB,KAAK,CAAC,IAAY,EAAE,MAAc,EAAE,MAAc;;QACtE,IAAI;YACF,MAAM,YAAY,EAAE,CAAC;YACpB,MAAc,CAAC,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;SAC3C;QAAC,OAAO,CAAC,EAAE;YACV,OAAO,CAAC,KAAK,CAAC,sCAAsC,CAAC,CAAC;SACvD;IACH,CAAC;CAAA;AAPD,sBAOC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/****************************************************************************************
|
|
2
|
+
Initialization
|
|
3
|
+
****************************************************************************************/
|
|
4
|
+
export declare function initialize(googleAnalyticsId?: string | null): void;
|
|
5
|
+
/****************************************************************************************
|
|
6
|
+
Tracking
|
|
7
|
+
****************************************************************************************/
|
|
8
|
+
export declare function track(type: string, action: string, params: object): Promise<void>;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.track = exports.initialize = void 0;
|
|
13
|
+
const Wait = require("./wait");
|
|
14
|
+
const SELLOUT_GOOGLE_ANALYTICS_ID = 'UA-112750612-5';
|
|
15
|
+
const SELLOUT_GOOGLE_TAG_ID = 'GTM-WX7FFS6';
|
|
16
|
+
function waitForGTag() {
|
|
17
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
18
|
+
return yield Wait.forTrue(() => Boolean(window.gtag));
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
/****************************************************************************************
|
|
22
|
+
Initialization
|
|
23
|
+
****************************************************************************************/
|
|
24
|
+
function initialize(googleAnalyticsId) {
|
|
25
|
+
try {
|
|
26
|
+
injectGoogleAnalytics(SELLOUT_GOOGLE_ANALYTICS_ID);
|
|
27
|
+
injectGoogleTagManager(SELLOUT_GOOGLE_TAG_ID);
|
|
28
|
+
if (googleAnalyticsId) {
|
|
29
|
+
injectGoogleAnalytics(googleAnalyticsId);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
console.log('Google Analytics not configured');
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
catch (e) {
|
|
36
|
+
console.error('Failed to inject Google Analaytics');
|
|
37
|
+
console.error(e);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.initialize = initialize;
|
|
41
|
+
function injectGoogleAnalytics(googleAnalyticsId) {
|
|
42
|
+
const headCode = `
|
|
43
|
+
< !--Global site tag(gtag.js) - Google Analytics-- >
|
|
44
|
+
<script async src="https://www.googletagmanager.com/gtag/js?id=${googleAnalyticsId}"></script>
|
|
45
|
+
<script>
|
|
46
|
+
window.dataLayer = window.dataLayer || [];
|
|
47
|
+
function gtag(){dataLayer.push(arguments);}
|
|
48
|
+
gtag('js', new Date());
|
|
49
|
+
gtag('config', '${googleAnalyticsId}');
|
|
50
|
+
</script>
|
|
51
|
+
`;
|
|
52
|
+
const headScript = document.createRange().createContextualFragment(headCode);
|
|
53
|
+
document.head.prepend(headScript);
|
|
54
|
+
}
|
|
55
|
+
function injectGoogleTagManager(googleTagManagerAccountId) {
|
|
56
|
+
const headCode = `
|
|
57
|
+
<!-- Google Tag Manager -->
|
|
58
|
+
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
|
|
59
|
+
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
|
|
60
|
+
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
|
|
61
|
+
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
|
|
62
|
+
})(window,document,'script','dataLayer','${googleTagManagerAccountId}');</script>
|
|
63
|
+
<!-- End Google Tag Manager -->
|
|
64
|
+
`;
|
|
65
|
+
const headScript = document.createRange().createContextualFragment(headCode);
|
|
66
|
+
document.head.prepend(headScript);
|
|
67
|
+
const bodyCode = `
|
|
68
|
+
<!-- Google Tag Manager (noscript) -->
|
|
69
|
+
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=${googleTagManagerAccountId}"
|
|
70
|
+
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
|
|
71
|
+
<!-- End Google Tag Manager (noscript) -->
|
|
72
|
+
`;
|
|
73
|
+
const bodyScript = document.createRange().createContextualFragment(bodyCode);
|
|
74
|
+
document.body.prepend(bodyScript);
|
|
75
|
+
}
|
|
76
|
+
/****************************************************************************************
|
|
77
|
+
Tracking
|
|
78
|
+
****************************************************************************************/
|
|
79
|
+
function track(type, action, params) {
|
|
80
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
81
|
+
try {
|
|
82
|
+
yield waitForGTag();
|
|
83
|
+
window.gtag(type, action, params);
|
|
84
|
+
}
|
|
85
|
+
catch (e) {
|
|
86
|
+
console.error('Google Analytics failed to track event');
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
exports.track = track;
|
|
91
|
+
//# sourceMappingURL=GoogleUtil.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GoogleUtil.js","sourceRoot":"","sources":["../src/GoogleUtil.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,+BAA+B;AAE/B,MAAM,2BAA2B,GAAG,gBAAgB,CAAC;AACrD,MAAM,qBAAqB,GAAG,aAAa,CAAC;AAE5C,SAAe,WAAW;;QACxB,OAAO,MAAM,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,OAAO,CAAE,MAAc,CAAC,IAAI,CAAC,CAAC,CAAC;IACjE,CAAC;CAAA;AAED;;yFAEyF;AAEzF,SAAgB,UAAU,CAAC,iBAAiC;IAC1D,IAAI;QACF,qBAAqB,CAAC,2BAA2B,CAAC,CAAC;QACnD,sBAAsB,CAAC,qBAAqB,CAAC,CAAC;QAC9C,IAAI,iBAAiB,EAAE;YACrB,qBAAqB,CAAC,iBAAiB,CAAC,CAAC;SAC1C;aAAM;YACL,OAAO,CAAC,GAAG,CAAC,iCAAiC,CAAC,CAAC;SAChD;KACF;IAAC,OAAO,CAAC,EAAE;QACV,OAAO,CAAC,KAAK,CAAC,oCAAoC,CAAC,CAAC;QACpD,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;KAClB;AACH,CAAC;AAbD,gCAaC;AAED,SAAS,qBAAqB,CAAC,iBAAyB;IACtD,MAAM,QAAQ,GAAG;;qEAEkD,iBAAiB;;;;;wBAK9D,iBAAiB;;GAEtC,CAAC;IAEF,MAAM,UAAU,GAAG,QAAQ,CAAC,WAAW,EAAE,CAAC,wBAAwB,CAAC,QAAQ,CAAC,CAAC;IAC7E,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;AACpC,CAAC;AAED,SAAS,sBAAsB,CAAC,yBAAiC;IAC/D,MAAM,QAAQ,GAAG;;;;;;iDAM8B,yBAAyB;;KAErE,CAAC;IAEJ,MAAM,UAAU,GAAG,QAAQ,CAAC,WAAW,EAAE,CAAC,wBAAwB,CAAC,QAAQ,CAAC,CAAC;IAC7E,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;IAElC,MAAM,QAAQ,GAAG;;yEAEsD,yBAAyB;;;GAG/F,CAAC;IAEF,MAAM,UAAU,GAAG,QAAQ,CAAC,WAAW,EAAE,CAAC,wBAAwB,CAAC,QAAQ,CAAC,CAAC;IAC7E,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;AACpC,CAAC;AAED;;yFAEyF;AAEzF,SAAsB,KAAK,CAAC,IAAY,EAAE,MAAc,EAAE,MAAc;;QACtE,IAAI;YACF,MAAM,WAAW,EAAE,CAAC;YACnB,MAAc,CAAC,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;SAC5C;QAAC,OAAO,CAAC,EAAE;YACV,OAAO,CAAC,KAAK,CAAC,wCAAwC,CAAC,CAAC;SACzD;IACH,CAAC;CAAA;AAPD,sBAOC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sellout/utils",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.73",
|
|
4
4
|
"description": "Sellout.io utils",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"prepare": "npm run build",
|
|
@@ -14,8 +14,8 @@
|
|
|
14
14
|
"author": "samheutmaker@gmail.com",
|
|
15
15
|
"license": "MIT",
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@sellout/models": "^0.0.
|
|
18
|
-
"@sellout/service": "^0.0.
|
|
17
|
+
"@sellout/models": "^0.0.73",
|
|
18
|
+
"@sellout/service": "^0.0.73",
|
|
19
19
|
"json2csv": "^5.0.0",
|
|
20
20
|
"moment-timezone": "^0.5.28",
|
|
21
21
|
"request": "^2.88.2",
|
|
@@ -26,5 +26,5 @@
|
|
|
26
26
|
"nodemon": "^2.0.2",
|
|
27
27
|
"typescript": "^3.8.3"
|
|
28
28
|
},
|
|
29
|
-
"gitHead": "
|
|
29
|
+
"gitHead": "666f85457f55bbb2cf458f092e1013fc961de007"
|
|
30
30
|
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import * as GoogleUtil from './GoogleUtil';
|
|
2
|
+
import * as FacebookUtil from './FacebookUtil';
|
|
3
|
+
|
|
4
|
+
export enum ClientTrackingEventTypes {
|
|
5
|
+
Purchase = 'Purchase',
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export interface IClientTrackingIds {
|
|
9
|
+
googleAnalyticsId?: string;
|
|
10
|
+
facebookPixelId?: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function initialize(clientTrackingIds: IClientTrackingIds) {
|
|
14
|
+
GoogleUtil.initialize(clientTrackingIds.googleAnalyticsId);
|
|
15
|
+
FacebookUtil.initialize(clientTrackingIds.facebookPixelId);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function track(type: ClientTrackingEventTypes, params: any) {
|
|
19
|
+
switch (type) {
|
|
20
|
+
case ClientTrackingEventTypes.Purchase:
|
|
21
|
+
return trackPurchase(params);
|
|
22
|
+
default:
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function trackPurchase({
|
|
28
|
+
order,
|
|
29
|
+
orderTotal,
|
|
30
|
+
channel,
|
|
31
|
+
currency = 'USD',
|
|
32
|
+
}): void {
|
|
33
|
+
GoogleUtil.track('event', 'purchase', {
|
|
34
|
+
transaction_id: order._id,
|
|
35
|
+
value: orderTotal / 100,
|
|
36
|
+
affiliation: channel,
|
|
37
|
+
currency,
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
FacebookUtil.track('track', 'Purchase', {
|
|
41
|
+
value: orderTotal / 100,
|
|
42
|
+
currency,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import * as Wait from './wait';
|
|
2
|
+
|
|
3
|
+
const SELLOUT_FACEBOOK_PIXEL_ID = '205397967210775';
|
|
4
|
+
|
|
5
|
+
async function waitForPixel() {
|
|
6
|
+
return await Wait.forTrue(() => Boolean((window as any).fbq));
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/****************************************************************************************
|
|
10
|
+
Initialization
|
|
11
|
+
****************************************************************************************/
|
|
12
|
+
|
|
13
|
+
export function initialize(facebookPixelId?: string | null): void {
|
|
14
|
+
try {
|
|
15
|
+
injectPixel(SELLOUT_FACEBOOK_PIXEL_ID);
|
|
16
|
+
if (facebookPixelId) {
|
|
17
|
+
injectPixel(facebookPixelId);
|
|
18
|
+
} else {
|
|
19
|
+
console.log('Facebook Pixel not configured');
|
|
20
|
+
}
|
|
21
|
+
} catch (e) {
|
|
22
|
+
console.error('Failed to inject Facebook Pixel');
|
|
23
|
+
console.error(e);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function injectPixel(facebookPixelId: string): void {
|
|
28
|
+
const headCode = `
|
|
29
|
+
< !--Facebook Pixel Code-- >
|
|
30
|
+
<script>
|
|
31
|
+
!function(f,b,e,v,n,t,s)
|
|
32
|
+
{if(f.fbq)return;n=f.fbq=function(){n.callMethod ?
|
|
33
|
+
n.callMethod.apply(n, arguments) : n.queue.push(arguments)};
|
|
34
|
+
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
|
|
35
|
+
n.queue=[];t=b.createElement(e);t.async=!0;
|
|
36
|
+
t.src=v;s=b.getElementsByTagName(e)[0];
|
|
37
|
+
s.parentNode.insertBefore(t,s)}(window, document,'script',
|
|
38
|
+
'https://connect.facebook.net/en_US/fbevents.js');
|
|
39
|
+
fbq('init', '${facebookPixelId}');
|
|
40
|
+
fbq('track', 'PageView');
|
|
41
|
+
</script>
|
|
42
|
+
<noscript><img height="1" width="1" style="display:none"
|
|
43
|
+
src="https://www.facebook.com/tr?id=${facebookPixelId}&ev=PageView&noscript=1"
|
|
44
|
+
/></noscript>
|
|
45
|
+
<!--End Facebook Pixel Code-- >
|
|
46
|
+
`;
|
|
47
|
+
|
|
48
|
+
const headScript = document.createRange().createContextualFragment(headCode);
|
|
49
|
+
document.head.prepend(headScript);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/****************************************************************************************
|
|
53
|
+
Tracking
|
|
54
|
+
****************************************************************************************/
|
|
55
|
+
|
|
56
|
+
export async function track(type: string, action: string, params: object): Promise<void> {
|
|
57
|
+
try {
|
|
58
|
+
await waitForPixel();
|
|
59
|
+
(window as any).fbq(type, action, params);
|
|
60
|
+
} catch (e) {
|
|
61
|
+
console.error('Facebook Pixel failed to track event');
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import * as Wait from './wait';
|
|
2
|
+
|
|
3
|
+
const SELLOUT_GOOGLE_ANALYTICS_ID = 'UA-112750612-5';
|
|
4
|
+
const SELLOUT_GOOGLE_TAG_ID = 'GTM-WX7FFS6';
|
|
5
|
+
|
|
6
|
+
async function waitForGTag() {
|
|
7
|
+
return await Wait.forTrue(() => Boolean((window as any).gtag));
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/****************************************************************************************
|
|
11
|
+
Initialization
|
|
12
|
+
****************************************************************************************/
|
|
13
|
+
|
|
14
|
+
export function initialize(googleAnalyticsId?: string | null): void {
|
|
15
|
+
try {
|
|
16
|
+
injectGoogleAnalytics(SELLOUT_GOOGLE_ANALYTICS_ID);
|
|
17
|
+
injectGoogleTagManager(SELLOUT_GOOGLE_TAG_ID);
|
|
18
|
+
if (googleAnalyticsId) {
|
|
19
|
+
injectGoogleAnalytics(googleAnalyticsId);
|
|
20
|
+
} else {
|
|
21
|
+
console.log('Google Analytics not configured');
|
|
22
|
+
}
|
|
23
|
+
} catch (e) {
|
|
24
|
+
console.error('Failed to inject Google Analaytics');
|
|
25
|
+
console.error(e);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function injectGoogleAnalytics(googleAnalyticsId: string): void {
|
|
30
|
+
const headCode = `
|
|
31
|
+
< !--Global site tag(gtag.js) - Google Analytics-- >
|
|
32
|
+
<script async src="https://www.googletagmanager.com/gtag/js?id=${googleAnalyticsId}"></script>
|
|
33
|
+
<script>
|
|
34
|
+
window.dataLayer = window.dataLayer || [];
|
|
35
|
+
function gtag(){dataLayer.push(arguments);}
|
|
36
|
+
gtag('js', new Date());
|
|
37
|
+
gtag('config', '${googleAnalyticsId}');
|
|
38
|
+
</script>
|
|
39
|
+
`;
|
|
40
|
+
|
|
41
|
+
const headScript = document.createRange().createContextualFragment(headCode);
|
|
42
|
+
document.head.prepend(headScript);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function injectGoogleTagManager(googleTagManagerAccountId: string): void {
|
|
46
|
+
const headCode = `
|
|
47
|
+
<!-- Google Tag Manager -->
|
|
48
|
+
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
|
|
49
|
+
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
|
|
50
|
+
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
|
|
51
|
+
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
|
|
52
|
+
})(window,document,'script','dataLayer','${googleTagManagerAccountId}');</script>
|
|
53
|
+
<!-- End Google Tag Manager -->
|
|
54
|
+
`;
|
|
55
|
+
|
|
56
|
+
const headScript = document.createRange().createContextualFragment(headCode);
|
|
57
|
+
document.head.prepend(headScript);
|
|
58
|
+
|
|
59
|
+
const bodyCode = `
|
|
60
|
+
<!-- Google Tag Manager (noscript) -->
|
|
61
|
+
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=${googleTagManagerAccountId}"
|
|
62
|
+
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
|
|
63
|
+
<!-- End Google Tag Manager (noscript) -->
|
|
64
|
+
`;
|
|
65
|
+
|
|
66
|
+
const bodyScript = document.createRange().createContextualFragment(bodyCode);
|
|
67
|
+
document.body.prepend(bodyScript);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/****************************************************************************************
|
|
71
|
+
Tracking
|
|
72
|
+
****************************************************************************************/
|
|
73
|
+
|
|
74
|
+
export async function track(type: string, action: string, params: object): Promise<void> {
|
|
75
|
+
try {
|
|
76
|
+
await waitForGTag();
|
|
77
|
+
(window as any).gtag(type, action, params);
|
|
78
|
+
} catch (e) {
|
|
79
|
+
console.error('Google Analytics failed to track event');
|
|
80
|
+
}
|
|
81
|
+
}
|