@amplitude/analytics-browser 0.4.1 → 0.6.0

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.
Files changed (111) hide show
  1. package/README.md +1 -1
  2. package/lib/cjs/attribution/campaign-parser.d.ts +10 -0
  3. package/lib/cjs/attribution/campaign-parser.d.ts.map +1 -0
  4. package/lib/cjs/attribution/campaign-parser.js +56 -0
  5. package/lib/cjs/attribution/campaign-parser.js.map +1 -0
  6. package/lib/cjs/attribution/campaign-tracker.d.ts +72 -0
  7. package/lib/cjs/attribution/campaign-tracker.d.ts.map +1 -0
  8. package/lib/cjs/attribution/campaign-tracker.js +91 -0
  9. package/lib/cjs/attribution/campaign-tracker.js.map +1 -0
  10. package/lib/cjs/{constants.d.ts → attribution/constants.d.ts} +4 -0
  11. package/lib/cjs/attribution/constants.d.ts.map +1 -0
  12. package/lib/cjs/attribution/constants.js +28 -0
  13. package/lib/cjs/attribution/constants.js.map +1 -0
  14. package/lib/cjs/browser-client.d.ts +17 -6
  15. package/lib/cjs/browser-client.d.ts.map +1 -1
  16. package/lib/cjs/browser-client.js +54 -29
  17. package/lib/cjs/browser-client.js.map +1 -1
  18. package/lib/cjs/config.d.ts +16 -16
  19. package/lib/cjs/config.d.ts.map +1 -1
  20. package/lib/cjs/config.js +99 -51
  21. package/lib/cjs/config.js.map +1 -1
  22. package/lib/cjs/cookie-migration/index.js +2 -2
  23. package/lib/cjs/cookie-migration/index.js.map +1 -1
  24. package/lib/cjs/plugins/context.d.ts +1 -0
  25. package/lib/cjs/plugins/context.d.ts.map +1 -1
  26. package/lib/cjs/plugins/context.js +22 -14
  27. package/lib/cjs/plugins/context.js.map +1 -1
  28. package/lib/cjs/session-manager.d.ts +19 -5
  29. package/lib/cjs/session-manager.d.ts.map +1 -1
  30. package/lib/cjs/session-manager.js +51 -33
  31. package/lib/cjs/session-manager.js.map +1 -1
  32. package/lib/cjs/utils/cookie-name.d.ts +3 -0
  33. package/lib/cjs/utils/cookie-name.d.ts.map +1 -0
  34. package/lib/cjs/utils/cookie-name.js +14 -0
  35. package/lib/cjs/utils/cookie-name.js.map +1 -0
  36. package/lib/cjs/version.d.ts +1 -1
  37. package/lib/cjs/version.js +1 -1
  38. package/lib/cjs/version.js.map +1 -1
  39. package/lib/esm/attribution/campaign-parser.d.ts +10 -0
  40. package/lib/esm/attribution/campaign-parser.d.ts.map +1 -0
  41. package/lib/esm/attribution/campaign-parser.js +54 -0
  42. package/lib/esm/attribution/campaign-parser.js.map +1 -0
  43. package/lib/esm/attribution/campaign-tracker.d.ts +72 -0
  44. package/lib/esm/attribution/campaign-tracker.d.ts.map +1 -0
  45. package/lib/esm/attribution/campaign-tracker.js +89 -0
  46. package/lib/esm/attribution/campaign-tracker.js.map +1 -0
  47. package/lib/esm/{constants.d.ts → attribution/constants.d.ts} +4 -0
  48. package/lib/esm/attribution/constants.d.ts.map +1 -0
  49. package/lib/esm/{constants.js → attribution/constants.js} +13 -0
  50. package/lib/esm/attribution/constants.js.map +1 -0
  51. package/lib/esm/browser-client.d.ts +17 -6
  52. package/lib/esm/browser-client.d.ts.map +1 -1
  53. package/lib/esm/browser-client.js +55 -30
  54. package/lib/esm/browser-client.js.map +1 -1
  55. package/lib/esm/config.d.ts +16 -16
  56. package/lib/esm/config.d.ts.map +1 -1
  57. package/lib/esm/config.js +93 -45
  58. package/lib/esm/config.js.map +1 -1
  59. package/lib/esm/cookie-migration/index.js +1 -1
  60. package/lib/esm/cookie-migration/index.js.map +1 -1
  61. package/lib/esm/plugins/context.d.ts +1 -0
  62. package/lib/esm/plugins/context.d.ts.map +1 -1
  63. package/lib/esm/plugins/context.js +22 -14
  64. package/lib/esm/plugins/context.js.map +1 -1
  65. package/lib/esm/session-manager.d.ts +19 -5
  66. package/lib/esm/session-manager.d.ts.map +1 -1
  67. package/lib/esm/session-manager.js +50 -28
  68. package/lib/esm/session-manager.js.map +1 -1
  69. package/lib/esm/utils/cookie-name.d.ts +3 -0
  70. package/lib/esm/utils/cookie-name.d.ts.map +1 -0
  71. package/lib/esm/utils/cookie-name.js +10 -0
  72. package/lib/esm/utils/cookie-name.js.map +1 -0
  73. package/lib/esm/version.d.ts +1 -1
  74. package/lib/esm/version.js +1 -1
  75. package/lib/esm/version.js.map +1 -1
  76. package/lib/scripts/amplitude-min.js +1 -1
  77. package/lib/scripts/amplitude-min.js.gz +0 -0
  78. package/lib/scripts/amplitude-min.umd.js +1 -1
  79. package/lib/scripts/amplitude-min.umd.js.gz +0 -0
  80. package/lib/scripts/amplitude-snippet-instructions.html +1 -1
  81. package/lib/scripts/amplitude-snippet-min.js +1 -1
  82. package/package.json +5 -5
  83. package/lib/cjs/attribution.d.ts +0 -11
  84. package/lib/cjs/attribution.d.ts.map +0 -1
  85. package/lib/cjs/attribution.js +0 -64
  86. package/lib/cjs/attribution.js.map +0 -1
  87. package/lib/cjs/constants.d.ts.map +0 -1
  88. package/lib/cjs/constants.js +0 -15
  89. package/lib/cjs/constants.js.map +0 -1
  90. package/lib/cjs/storage/memory.d.ts +0 -11
  91. package/lib/cjs/storage/memory.d.ts.map +0 -1
  92. package/lib/cjs/storage/memory.js +0 -29
  93. package/lib/cjs/storage/memory.js.map +0 -1
  94. package/lib/cjs/utils/uuid.d.ts +0 -10
  95. package/lib/cjs/utils/uuid.d.ts.map +0 -1
  96. package/lib/cjs/utils/uuid.js +0 -31
  97. package/lib/cjs/utils/uuid.js.map +0 -1
  98. package/lib/esm/attribution.d.ts +0 -11
  99. package/lib/esm/attribution.d.ts.map +0 -1
  100. package/lib/esm/attribution.js +0 -57
  101. package/lib/esm/attribution.js.map +0 -1
  102. package/lib/esm/constants.d.ts.map +0 -1
  103. package/lib/esm/constants.js.map +0 -1
  104. package/lib/esm/storage/memory.d.ts +0 -11
  105. package/lib/esm/storage/memory.d.ts.map +0 -1
  106. package/lib/esm/storage/memory.js +0 -27
  107. package/lib/esm/storage/memory.js.map +0 -1
  108. package/lib/esm/utils/uuid.d.ts +0 -10
  109. package/lib/esm/utils/uuid.d.ts.map +0 -1
  110. package/lib/esm/utils/uuid.js +0 -28
  111. package/lib/esm/utils/uuid.js.map +0 -1
package/README.md CHANGED
@@ -32,7 +32,7 @@ Alternatively, the package is also distributed through a CDN. Copy and paste the
32
32
  <!-- README_SNIPPET_BLOCK -->
33
33
  ```html
34
34
  <script type="text/javascript">
35
- !function(){"use strict";!function(e,t){var r=e.amplitude||{_q:[]};if(r.invoked)e.console&&console.error&&console.error("Amplitude snippet has been loaded.");else{r.invoked=!0;var n=t.createElement("script");n.type="text/javascript",n.integrity="sha384-uhLfRZptXuAOheWdBzdbLlI4IsiZKZDJU5F0tF0QADF4vMob3RWlNU4DJ9PZV2J4",n.crossOrigin="anonymous",n.async=!0,n.src="https://cdn.amplitude.com/libs/analytics-browser-0.4.1-min.js.gz",n.onload=function(){e.amplitude.runQueuedFunctions||console.log("[Amplitude] Error: could not load SDK")};var s=t.getElementsByTagName("script")[0];function v(e,t){e.prototype[t]=function(){return this._q.push({name:t,args:Array.prototype.slice.call(arguments,0)}),this}}s.parentNode.insertBefore(n,s);for(var o=function(){return this._q=[],this},i=["add","append","clearAll","prepend","set","setOnce","unset","preInsert","postInsert","remove","getUserProperties"],a=0;a<i.length;a++)v(o,i[a]);r.Identify=o;for(var u=function(){return this._q=[],this},c=["getEventProperties","setProductId","setQuantity","setPrice","setRevenue","setRevenueType","setEventProperties"],p=0;p<c.length;p++)v(u,c[p]);r.Revenue=u;var d=["getDeviceId","setDeviceId","getSessionId","setSessionId","getUserId","setUserId","setOptOut","setTransport"],l=["init","add","remove","track","logEvent","identify","groupIdentify","setGroup","revenue"];function f(e){function t(t,r){e[t]=function(){var n={promise:new Promise((r=>{e._q.push({name:t,args:Array.prototype.slice.call(arguments,0),resolve:r})}))};if(r)return n}}for(var r=0;r<d.length;r++)t(d[r],!1);for(var n=0;n<l.length;n++)t(l[n],!0)}f(r),e.amplitude=r}}(window,document)}();
35
+ !function(){"use strict";!function(e,t){var r=e.amplitude||{_q:[]};if(r.invoked)e.console&&console.error&&console.error("Amplitude snippet has been loaded.");else{r.invoked=!0;var n=t.createElement("script");n.type="text/javascript",n.integrity="sha384-4iw/ApE3sdQC1CWOXWWaMtSWy/RhSRL8yhTRD/S0bpSiCLS60Y9uZFv3C48RiB8U",n.crossOrigin="anonymous",n.async=!0,n.src="https://cdn.amplitude.com/libs/analytics-browser-0.6.0-min.js.gz",n.onload=function(){e.amplitude.runQueuedFunctions||console.log("[Amplitude] Error: could not load SDK")};var s=t.getElementsByTagName("script")[0];function v(e,t){e.prototype[t]=function(){return this._q.push({name:t,args:Array.prototype.slice.call(arguments,0)}),this}}s.parentNode.insertBefore(n,s);for(var o=function(){return this._q=[],this},i=["add","append","clearAll","prepend","set","setOnce","unset","preInsert","postInsert","remove","getUserProperties"],a=0;a<i.length;a++)v(o,i[a]);r.Identify=o;for(var u=function(){return this._q=[],this},c=["getEventProperties","setProductId","setQuantity","setPrice","setRevenue","setRevenueType","setEventProperties"],p=0;p<c.length;p++)v(u,c[p]);r.Revenue=u;var d=["getDeviceId","setDeviceId","regenerateDeviceId","getSessionId","setSessionId","getUserId","setUserId","setOptOut","setTransport"],l=["init","add","remove","track","logEvent","identify","groupIdentify","setGroup","revenue"];function f(e){function t(t,r){e[t]=function(){var n={promise:new Promise((r=>{e._q.push({name:t,args:Array.prototype.slice.call(arguments,0),resolve:r})}))};if(r)return n}}for(var r=0;r<d.length;r++)t(d[r],!1);for(var n=0;n<l.length;n++)t(l[n],!0)}f(r),e.amplitude=r}}(window,document)}();
36
36
 
37
37
  amplitude.init("YOUR_API_KEY_HERE");
38
38
  </script>
@@ -0,0 +1,10 @@
1
+ import { UTMCookie } from '../storage/utm-cookie';
2
+ import { Campaign, CampaignParser as ICampaignParser, ClickIdParameters, ReferrerParameters, UTMParameters } from '@amplitude/analytics-types';
3
+ export declare class CampaignParser implements ICampaignParser {
4
+ utmCookieStorage: UTMCookie;
5
+ parse(): Campaign;
6
+ getUtmParam(): UTMParameters;
7
+ getReferrer(): ReferrerParameters;
8
+ getClickIds(): ClickIdParameters;
9
+ }
10
+ //# sourceMappingURL=campaign-parser.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"campaign-parser.d.ts","sourceRoot":"","sources":["../../../src/attribution/campaign-parser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAiBlD,OAAO,EACL,QAAQ,EACR,cAAc,IAAI,eAAe,EACjC,iBAAiB,EACjB,kBAAkB,EAClB,aAAa,EACd,MAAM,4BAA4B,CAAC;AAEpC,qBAAa,cAAe,YAAW,eAAe;IACpD,gBAAgB,YAAmB;IAEnC,KAAK,IAAI,QAAQ;IASjB,WAAW,IAAI,aAAa;IAmB5B,WAAW,IAAI,kBAAkB;IAcjC,WAAW,IAAI,iBAAiB;CAOjC"}
@@ -0,0 +1,56 @@
1
+ Object.defineProperty(exports, "__esModule", { value: true });
2
+ exports.CampaignParser = void 0;
3
+ var tslib_1 = require("tslib");
4
+ var utm_cookie_1 = require("../storage/utm-cookie");
5
+ var query_params_1 = require("../utils/query-params");
6
+ var constants_1 = require("./constants");
7
+ var CampaignParser = /** @class */ (function () {
8
+ function CampaignParser() {
9
+ this.utmCookieStorage = new utm_cookie_1.UTMCookie();
10
+ }
11
+ CampaignParser.prototype.parse = function () {
12
+ return (0, tslib_1.__assign)((0, tslib_1.__assign)((0, tslib_1.__assign)((0, tslib_1.__assign)({}, constants_1.BASE_CAMPAIGN), this.getUtmParam()), this.getReferrer()), this.getClickIds());
13
+ };
14
+ CampaignParser.prototype.getUtmParam = function () {
15
+ var params = (0, query_params_1.getQueryParams)();
16
+ var cookies = (this.utmCookieStorage.isEnabled() && this.utmCookieStorage.get('__utmz')) || {};
17
+ var utmSource = params[constants_1.UTM_SOURCE] || cookies[constants_1.UTMZ_SOURCE];
18
+ var utmMedium = params[constants_1.UTM_MEDIUM] || cookies[constants_1.UTMZ_MEDIUM];
19
+ var utmCampaign = params[constants_1.UTM_CAMPAIGN] || cookies[constants_1.UTMZ_CAMPAIGN];
20
+ var utmTerm = params[constants_1.UTM_TERM] || cookies[constants_1.UTMZ_TERM];
21
+ var utmContent = params[constants_1.UTM_CONTENT] || cookies[constants_1.UTMZ_CONTENT];
22
+ return {
23
+ utm_source: utmSource,
24
+ utm_medium: utmMedium,
25
+ utm_campaign: utmCampaign,
26
+ utm_term: utmTerm,
27
+ utm_content: utmContent,
28
+ };
29
+ };
30
+ CampaignParser.prototype.getReferrer = function () {
31
+ var _a, _b;
32
+ var data = {
33
+ referrer: undefined,
34
+ referring_domain: undefined,
35
+ };
36
+ try {
37
+ data.referrer = document.referrer || undefined;
38
+ data.referring_domain = (_b = (_a = data.referrer) === null || _a === void 0 ? void 0 : _a.split('/')[2]) !== null && _b !== void 0 ? _b : undefined;
39
+ }
40
+ catch (_c) {
41
+ // nothing to track
42
+ }
43
+ return data;
44
+ };
45
+ CampaignParser.prototype.getClickIds = function () {
46
+ var _a;
47
+ var params = (0, query_params_1.getQueryParams)();
48
+ return _a = {},
49
+ _a[constants_1.GCLID] = params[constants_1.GCLID],
50
+ _a[constants_1.FBCLID] = params[constants_1.FBCLID],
51
+ _a;
52
+ };
53
+ return CampaignParser;
54
+ }());
55
+ exports.CampaignParser = CampaignParser;
56
+ //# sourceMappingURL=campaign-parser.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"campaign-parser.js","sourceRoot":"","sources":["../../../src/attribution/campaign-parser.ts"],"names":[],"mappings":";;;AAAA,oDAAkD;AAClD,sDAAuD;AACvD,yCAcqB;AASrB;IAAA;QACE,qBAAgB,GAAG,IAAI,sBAAS,EAAE,CAAC;IAmDrC,CAAC;IAjDC,8BAAK,GAAL;QACE,mGACK,yBAAa,GACb,IAAI,CAAC,WAAW,EAAE,GAClB,IAAI,CAAC,WAAW,EAAE,GAClB,IAAI,CAAC,WAAW,EAAE,EACrB;IACJ,CAAC;IAED,oCAAW,GAAX;QACE,IAAM,MAAM,GAAG,IAAA,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AAC,iBAAK,IAAG,MAAM,CAAC,iBAAK,CAAC;YACtB,GAAC,kBAAM,IAAG,MAAM,CAAC,kBAAM,CAAC;eACxB;IACJ,CAAC;IACH,qBAAC;AAAD,CAAC,AApDD,IAoDC;AApDY,wCAAc","sourcesContent":["import { UTMCookie } from '../storage/utm-cookie';\nimport { getQueryParams } from '../utils/query-params';\nimport {\n UTM_CAMPAIGN,\n UTM_CONTENT,\n UTM_MEDIUM,\n UTM_SOURCE,\n UTM_TERM,\n UTMZ_SOURCE,\n UTMZ_MEDIUM,\n UTMZ_CAMPAIGN,\n UTMZ_TERM,\n UTMZ_CONTENT,\n GCLID,\n FBCLID,\n BASE_CAMPAIGN,\n} from './constants';\nimport {\n Campaign,\n CampaignParser as ICampaignParser,\n ClickIdParameters,\n ReferrerParameters,\n UTMParameters,\n} from '@amplitude/analytics-types';\n\nexport class CampaignParser implements ICampaignParser {\n utmCookieStorage = new UTMCookie();\n\n parse(): Campaign {\n return {\n ...BASE_CAMPAIGN,\n ...this.getUtmParam(),\n ...this.getReferrer(),\n ...this.getClickIds(),\n };\n }\n\n getUtmParam(): UTMParameters {\n const params = getQueryParams();\n const cookies = (this.utmCookieStorage.isEnabled() && this.utmCookieStorage.get('__utmz')) || {};\n\n const utmSource = params[UTM_SOURCE] || cookies[UTMZ_SOURCE];\n const utmMedium = params[UTM_MEDIUM] || cookies[UTMZ_MEDIUM];\n const utmCampaign = params[UTM_CAMPAIGN] || cookies[UTMZ_CAMPAIGN];\n const utmTerm = params[UTM_TERM] || cookies[UTMZ_TERM];\n const utmContent = params[UTM_CONTENT] || cookies[UTMZ_CONTENT];\n\n return {\n utm_source: utmSource,\n utm_medium: utmMedium,\n utm_campaign: utmCampaign,\n utm_term: utmTerm,\n utm_content: utmContent,\n };\n }\n\n getReferrer(): ReferrerParameters {\n const data: ReferrerParameters = {\n referrer: undefined,\n referring_domain: undefined,\n };\n try {\n data.referrer = document.referrer || undefined;\n data.referring_domain = data.referrer?.split('/')[2] ?? undefined;\n } catch {\n // nothing to track\n }\n return data;\n }\n\n getClickIds(): ClickIdParameters {\n const params = getQueryParams();\n return {\n [GCLID]: params[GCLID],\n [FBCLID]: params[FBCLID],\n };\n }\n}\n"]}
@@ -0,0 +1,72 @@
1
+ import { Storage, Campaign, CampaignParser as ICampaignParser, CampaignTracker as ICampaignTracker, CampaignTrackFunction, CampaignTrackerOptions } from '@amplitude/analytics-types';
2
+ export declare class CampaignTracker implements ICampaignTracker {
3
+ storage: Storage<Campaign>;
4
+ storageKey: string;
5
+ parser: ICampaignParser;
6
+ track: CampaignTrackFunction;
7
+ onNewCampaign: (campaign: Campaign) => unknown;
8
+ disabled: boolean;
9
+ trackNewCampaigns: boolean;
10
+ trackPageViews: boolean;
11
+ excludeReferrers: string[];
12
+ initialEmptyValue: string;
13
+ constructor(apiKey: string, options: CampaignTrackerOptions);
14
+ isNewCampaign(currentCampaign: Campaign, previousCampaign: Campaign): boolean;
15
+ saveCampaignToStorage(campaign: Campaign): void;
16
+ getCampaignFromStorage(): Campaign;
17
+ createCampaignEvent(campaign: Campaign): {
18
+ event_type: string;
19
+ event_properties?: {
20
+ [key: string]: any;
21
+ } | undefined;
22
+ user_properties: {
23
+ [key: string]: any;
24
+ } | import("@amplitude/analytics-types").IdentifyUserProperties | {
25
+ [x: string]: any;
26
+ };
27
+ group_properties?: {
28
+ [key: string]: any;
29
+ } | undefined;
30
+ groups?: {
31
+ [key: string]: any;
32
+ } | undefined;
33
+ user_id?: string | undefined;
34
+ device_id?: string | undefined;
35
+ time?: number | undefined;
36
+ location_lat?: number | undefined;
37
+ location_lng?: number | undefined;
38
+ app_version?: string | undefined;
39
+ version_name?: string | undefined;
40
+ library?: string | undefined;
41
+ platform?: string | undefined;
42
+ os_name?: string | undefined;
43
+ os_version?: string | undefined;
44
+ device_brand?: string | undefined;
45
+ device_manufacturer?: string | undefined;
46
+ device_model?: string | undefined;
47
+ carrier?: string | undefined;
48
+ country?: string | undefined;
49
+ region?: string | undefined;
50
+ city?: string | undefined;
51
+ dma?: string | undefined;
52
+ idfa?: string | undefined;
53
+ idfv?: string | undefined;
54
+ adid?: string | undefined;
55
+ android_id?: string | undefined;
56
+ language?: string | undefined;
57
+ ip?: string | undefined;
58
+ uuid?: string | undefined;
59
+ price?: number | undefined;
60
+ quantity?: number | undefined;
61
+ revenue?: number | undefined;
62
+ productId?: string | undefined;
63
+ revenueType?: string | undefined;
64
+ event_id?: number | undefined;
65
+ session_id?: number | undefined;
66
+ insert_id?: string | undefined;
67
+ plan?: import("@amplitude/analytics-types").Plan | undefined;
68
+ partner_id?: string | undefined;
69
+ };
70
+ send(isNewSession: boolean): Promise<void>;
71
+ }
72
+ //# sourceMappingURL=campaign-tracker.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"campaign-tracker.d.ts","sourceRoot":"","sources":["../../../src/attribution/campaign-tracker.ts"],"names":[],"mappings":"AACA,OAAO,EACL,OAAO,EACP,QAAQ,EACR,cAAc,IAAI,eAAe,EACjC,eAAe,IAAI,gBAAgB,EACnC,qBAAqB,EACrB,sBAAsB,EAEvB,MAAM,4BAA4B,CAAC;AAKpC,qBAAa,eAAgB,YAAW,gBAAgB;IACtD,OAAO,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC3B,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,eAAe,CAAC;IACxB,KAAK,EAAE,qBAAqB,CAAC;IAC7B,aAAa,EAAE,CAAC,QAAQ,EAAE,QAAQ,KAAK,OAAO,CAAC;IAE/C,QAAQ,EAAE,OAAO,CAAC;IAClB,iBAAiB,EAAE,OAAO,CAAC;IAC3B,cAAc,EAAE,OAAO,CAAC;IACxB,gBAAgB,EAAE,MAAM,EAAE,CAAC;IAC3B,iBAAiB,EAAE,MAAM,CAAC;gBAEd,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,sBAAsB;IAiB3D,aAAa,CAAC,eAAe,EAAE,QAAQ,EAAE,gBAAgB,EAAE,QAAQ;IAenE,qBAAqB,CAAC,QAAQ,EAAE,QAAQ;IAIxC,sBAAsB,IAAI,QAAQ;IAIlC,mBAAmB,CAAC,QAAQ,EAAE,QAAQ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IA6BhC,IAAI,CAAC,YAAY,EAAE,OAAO;CAejC"}
@@ -0,0 +1,91 @@
1
+ Object.defineProperty(exports, "__esModule", { value: true });
2
+ exports.CampaignTracker = void 0;
3
+ var tslib_1 = require("tslib");
4
+ var analytics_core_1 = require("@amplitude/analytics-core");
5
+ var cookie_name_1 = require("../utils/cookie-name");
6
+ var campaign_parser_1 = require("./campaign-parser");
7
+ var constants_1 = require("./constants");
8
+ var CampaignTracker = /** @class */ (function () {
9
+ function CampaignTracker(apiKey, options) {
10
+ var _a, _b;
11
+ this.storage = options.storage;
12
+ this.storageKey = (0, cookie_name_1.getCookieName)(apiKey, constants_1.MKTG);
13
+ this.parser = new campaign_parser_1.CampaignParser();
14
+ this.track = options.track;
15
+ this.onNewCampaign = options.onNewCampaign;
16
+ this.disabled = Boolean(options.disabled);
17
+ this.trackNewCampaigns = Boolean(options.trackNewCampaigns);
18
+ this.trackPageViews = options.trackPageViews === undefined || Boolean(options.trackPageViews);
19
+ this.excludeReferrers = (_a = options.excludeReferrers) !== null && _a !== void 0 ? _a : [];
20
+ if (typeof location !== 'undefined') {
21
+ this.excludeReferrers.unshift(location.hostname);
22
+ }
23
+ this.initialEmptyValue = (_b = options.initialEmptyValue) !== null && _b !== void 0 ? _b : constants_1.EMPTY_VALUE;
24
+ }
25
+ CampaignTracker.prototype.isNewCampaign = function (currentCampaign, previousCampaign) {
26
+ var isReferrerExcluded = Boolean(currentCampaign.referring_domain && this.excludeReferrers.includes(currentCampaign.referring_domain));
27
+ var hasNewUtm = previousCampaign.utm_campaign !== currentCampaign.utm_campaign ||
28
+ previousCampaign.utm_source !== currentCampaign.utm_source ||
29
+ previousCampaign.utm_medium !== currentCampaign.utm_medium ||
30
+ previousCampaign.utm_term !== currentCampaign.utm_term ||
31
+ previousCampaign.utm_content !== currentCampaign.utm_content;
32
+ var hasNewReferrer = previousCampaign.referring_domain !== currentCampaign.referring_domain;
33
+ return !isReferrerExcluded && (hasNewUtm || hasNewReferrer);
34
+ };
35
+ CampaignTracker.prototype.saveCampaignToStorage = function (campaign) {
36
+ this.storage.set(this.storageKey, campaign);
37
+ };
38
+ CampaignTracker.prototype.getCampaignFromStorage = function () {
39
+ return this.storage.get(this.storageKey) || (0, tslib_1.__assign)({}, constants_1.BASE_CAMPAIGN);
40
+ };
41
+ CampaignTracker.prototype.createCampaignEvent = function (campaign) {
42
+ var _this = this;
43
+ var campaignParameters = (0, tslib_1.__assign)((0, tslib_1.__assign)({}, constants_1.BASE_CAMPAIGN), campaign);
44
+ var identifyEvent = Object.entries(campaignParameters).reduce(function (identify, _a) {
45
+ var _b = (0, tslib_1.__read)(_a, 2), key = _b[0], value = _b[1];
46
+ identify.setOnce("initial_".concat(key), value || _this.initialEmptyValue);
47
+ if (value) {
48
+ return identify.set(key, value);
49
+ }
50
+ return identify.unset(key);
51
+ }, new analytics_core_1.Identify());
52
+ var pageViewEvent = {
53
+ event_type: 'Page View',
54
+ event_properties: {
55
+ page_title: /* istanbul ignore next */ (typeof document !== 'undefined' && document.title) || '',
56
+ page_location: /* istanbul ignore next */ (typeof location !== 'undefined' && location.href) || '',
57
+ page_path: /* istanbul ignore next */ (typeof location !== 'undefined' && location.pathname) || '',
58
+ },
59
+ };
60
+ return (0, tslib_1.__assign)((0, tslib_1.__assign)({}, (0, analytics_core_1.createIdentifyEvent)(identifyEvent)), (this.trackPageViews && pageViewEvent));
61
+ };
62
+ CampaignTracker.prototype.send = function (isNewSession) {
63
+ return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
64
+ var currentCampaign, previousCampaign;
65
+ return (0, tslib_1.__generator)(this, function (_a) {
66
+ switch (_a.label) {
67
+ case 0:
68
+ if (this.disabled) {
69
+ return [2 /*return*/];
70
+ }
71
+ currentCampaign = this.parser.parse();
72
+ previousCampaign = this.getCampaignFromStorage();
73
+ if (!isNewSession) {
74
+ if (!this.trackNewCampaigns || !this.isNewCampaign(currentCampaign, previousCampaign)) {
75
+ return [2 /*return*/];
76
+ }
77
+ this.onNewCampaign(currentCampaign);
78
+ }
79
+ return [4 /*yield*/, this.track(this.createCampaignEvent(currentCampaign))];
80
+ case 1:
81
+ _a.sent();
82
+ this.saveCampaignToStorage(currentCampaign);
83
+ return [2 /*return*/];
84
+ }
85
+ });
86
+ });
87
+ };
88
+ return CampaignTracker;
89
+ }());
90
+ exports.CampaignTracker = CampaignTracker;
91
+ //# sourceMappingURL=campaign-tracker.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"campaign-tracker.js","sourceRoot":"","sources":["../../../src/attribution/campaign-tracker.ts"],"names":[],"mappings":";;;AAAA,4DAA0E;AAU1E,oDAAsE;AACtE,qDAAmD;AACnD,yCAA+D;AAE/D;IAaE,yBAAY,MAAc,EAAE,OAA+B;;QACzD,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;QAC/B,IAAI,CAAC,UAAU,GAAG,IAAA,2BAAa,EAAC,MAAM,EAAE,gBAAI,CAAC,CAAC;QAC9C,IAAI,CAAC,MAAM,GAAG,IAAI,gCAAc,EAAE,CAAC;QACnC,IAAI,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC;QAC3B,IAAI,CAAC,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC;QAE3C,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;QAC1C,IAAI,CAAC,iBAAiB,GAAG,OAAO,CAAC,OAAO,CAAC,iBAAiB,CAAC,CAAC;QAC5D,IAAI,CAAC,cAAc,GAAG,OAAO,CAAC,cAAc,KAAK,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC,cAAc,CAAC,CAAC;QAC9F,IAAI,CAAC,gBAAgB,GAAG,MAAA,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C;wBACvD,IAAI,CAAC,YAAY,EAAE;4BACjB,IAAI,CAAC,IAAI,CAAC,iBAAiB,IAAI,CAAC,IAAI,CAAC,aAAa,CAAC,eAAe,EAAE,gBAAgB,CAAC,EAAE;gCACrF,sBAAO;6BACR;4BACD,IAAI,CAAC,aAAa,CAAC,eAAe,CAAC,CAAC;yBACrC;wBACD,qBAAM,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,mBAAmB,CAAC,eAAe,CAAC,CAAC,EAAA;;wBAA3D,SAA2D,CAAC;wBAC5D,IAAI,CAAC,qBAAqB,CAAC,eAAe,CAAC,CAAC;;;;;KAC7C;IACH,sBAAC;AAAD,CAAC,AAjGD,IAiGC;AAjGY,0CAAe","sourcesContent":["import { createIdentifyEvent, Identify } from '@amplitude/analytics-core';\nimport {\n Storage,\n Campaign,\n CampaignParser as ICampaignParser,\n CampaignTracker as ICampaignTracker,\n CampaignTrackFunction,\n CampaignTrackerOptions,\n BaseEvent,\n} from '@amplitude/analytics-types';\nimport { getCookieName as getStorageKey } from '../utils/cookie-name';\nimport { CampaignParser } from './campaign-parser';\nimport { BASE_CAMPAIGN, EMPTY_VALUE, MKTG } from './constants';\n\nexport class CampaignTracker implements ICampaignTracker {\n storage: Storage<Campaign>;\n storageKey: string;\n parser: ICampaignParser;\n track: CampaignTrackFunction;\n onNewCampaign: (campaign: Campaign) => unknown;\n\n disabled: boolean;\n trackNewCampaigns: boolean;\n trackPageViews: boolean;\n excludeReferrers: string[];\n initialEmptyValue: string;\n\n constructor(apiKey: string, options: CampaignTrackerOptions) {\n this.storage = options.storage;\n this.storageKey = getStorageKey(apiKey, MKTG);\n this.parser = new CampaignParser();\n this.track = options.track;\n this.onNewCampaign = options.onNewCampaign;\n\n this.disabled = Boolean(options.disabled);\n this.trackNewCampaigns = Boolean(options.trackNewCampaigns);\n this.trackPageViews = options.trackPageViews === undefined || Boolean(options.trackPageViews);\n this.excludeReferrers = options.excludeReferrers ?? [];\n if (typeof location !== 'undefined') {\n this.excludeReferrers.unshift(location.hostname);\n }\n this.initialEmptyValue = options.initialEmptyValue ?? EMPTY_VALUE;\n }\n\n isNewCampaign(currentCampaign: Campaign, previousCampaign: Campaign) {\n const isReferrerExcluded = Boolean(\n currentCampaign.referring_domain && this.excludeReferrers.includes(currentCampaign.referring_domain),\n );\n const hasNewUtm =\n previousCampaign.utm_campaign !== currentCampaign.utm_campaign ||\n previousCampaign.utm_source !== currentCampaign.utm_source ||\n previousCampaign.utm_medium !== currentCampaign.utm_medium ||\n previousCampaign.utm_term !== currentCampaign.utm_term ||\n previousCampaign.utm_content !== currentCampaign.utm_content;\n const hasNewReferrer = previousCampaign.referring_domain !== currentCampaign.referring_domain;\n\n return !isReferrerExcluded && (hasNewUtm || hasNewReferrer);\n }\n\n saveCampaignToStorage(campaign: Campaign) {\n this.storage.set(this.storageKey, campaign);\n }\n\n getCampaignFromStorage(): Campaign {\n return this.storage.get(this.storageKey) || { ...BASE_CAMPAIGN };\n }\n\n createCampaignEvent(campaign: Campaign) {\n const campaignParameters: Campaign = {\n // This object definition allows undefined keys to be iterated on\n // in .reduce() to build indentify object\n ...BASE_CAMPAIGN,\n ...campaign,\n };\n const identifyEvent = Object.entries(campaignParameters).reduce((identify, [key, value]) => {\n identify.setOnce(`initial_${key}`, value || this.initialEmptyValue);\n if (value) {\n return identify.set(key, value);\n }\n return identify.unset(key);\n }, new Identify());\n\n const pageViewEvent: BaseEvent = {\n event_type: 'Page View',\n event_properties: {\n page_title: /* istanbul ignore next */ (typeof document !== 'undefined' && document.title) || '',\n page_location: /* istanbul ignore next */ (typeof location !== 'undefined' && location.href) || '',\n page_path: /* istanbul ignore next */ (typeof location !== 'undefined' && location.pathname) || '',\n },\n };\n return {\n ...createIdentifyEvent(identifyEvent),\n ...(this.trackPageViews && pageViewEvent),\n };\n }\n\n async send(isNewSession: boolean) {\n if (this.disabled) {\n return;\n }\n const currentCampaign = this.parser.parse();\n const previousCampaign = this.getCampaignFromStorage();\n if (!isNewSession) {\n if (!this.trackNewCampaigns || !this.isNewCampaign(currentCampaign, previousCampaign)) {\n return;\n }\n this.onNewCampaign(currentCampaign);\n }\n await this.track(this.createCampaignEvent(currentCampaign));\n this.saveCampaignToStorage(currentCampaign);\n }\n}\n"]}
@@ -1,3 +1,4 @@
1
+ import { Campaign } from '@amplitude/analytics-types';
1
2
  export declare const UTM_SOURCE = "utm_source";
2
3
  export declare const UTM_MEDIUM = "utm_medium";
3
4
  export declare const UTM_CAMPAIGN = "utm_campaign";
@@ -10,4 +11,7 @@ export declare const UTMZ_TERM = "utmctr";
10
11
  export declare const UTMZ_CONTENT = "utmcct";
11
12
  export declare const GCLID = "gclid";
12
13
  export declare const FBCLID = "fbclid";
14
+ export declare const EMPTY_VALUE = "EMPTY";
15
+ export declare const BASE_CAMPAIGN: Campaign;
16
+ export declare const MKTG = "MKTG";
13
17
  //# sourceMappingURL=constants.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../../src/attribution/constants.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,4BAA4B,CAAC;AAEtD,eAAO,MAAM,UAAU,eAAe,CAAC;AACvC,eAAO,MAAM,UAAU,eAAe,CAAC;AACvC,eAAO,MAAM,YAAY,iBAAiB,CAAC;AAC3C,eAAO,MAAM,QAAQ,aAAa,CAAC;AACnC,eAAO,MAAM,WAAW,gBAAgB,CAAC;AAEzC,eAAO,MAAM,WAAW,WAAW,CAAC;AACpC,eAAO,MAAM,WAAW,WAAW,CAAC;AACpC,eAAO,MAAM,aAAa,WAAW,CAAC;AACtC,eAAO,MAAM,SAAS,WAAW,CAAC;AAClC,eAAO,MAAM,YAAY,WAAW,CAAC;AAErC,eAAO,MAAM,KAAK,UAAU,CAAC;AAC7B,eAAO,MAAM,MAAM,WAAW,CAAC;AAE/B,eAAO,MAAM,WAAW,UAAU,CAAC;AAEnC,eAAO,MAAM,aAAa,EAAE,QAU3B,CAAC;AAEF,eAAO,MAAM,IAAI,SAAS,CAAC"}
@@ -0,0 +1,28 @@
1
+ Object.defineProperty(exports, "__esModule", { value: true });
2
+ exports.MKTG = exports.BASE_CAMPAIGN = exports.EMPTY_VALUE = exports.FBCLID = exports.GCLID = exports.UTMZ_CONTENT = exports.UTMZ_TERM = exports.UTMZ_CAMPAIGN = exports.UTMZ_MEDIUM = exports.UTMZ_SOURCE = exports.UTM_CONTENT = exports.UTM_TERM = exports.UTM_CAMPAIGN = exports.UTM_MEDIUM = exports.UTM_SOURCE = void 0;
3
+ exports.UTM_SOURCE = 'utm_source';
4
+ exports.UTM_MEDIUM = 'utm_medium';
5
+ exports.UTM_CAMPAIGN = 'utm_campaign';
6
+ exports.UTM_TERM = 'utm_term';
7
+ exports.UTM_CONTENT = 'utm_content';
8
+ exports.UTMZ_SOURCE = 'utmcsr';
9
+ exports.UTMZ_MEDIUM = 'utmcmd';
10
+ exports.UTMZ_CAMPAIGN = 'utmccn';
11
+ exports.UTMZ_TERM = 'utmctr';
12
+ exports.UTMZ_CONTENT = 'utmcct';
13
+ exports.GCLID = 'gclid';
14
+ exports.FBCLID = 'fbclid';
15
+ exports.EMPTY_VALUE = 'EMPTY';
16
+ exports.BASE_CAMPAIGN = {
17
+ utm_source: undefined,
18
+ utm_medium: undefined,
19
+ utm_campaign: undefined,
20
+ utm_term: undefined,
21
+ utm_content: undefined,
22
+ referrer: undefined,
23
+ referring_domain: undefined,
24
+ gclid: undefined,
25
+ fbclid: undefined,
26
+ };
27
+ exports.MKTG = 'MKTG';
28
+ //# sourceMappingURL=constants.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../src/attribution/constants.ts"],"names":[],"mappings":";;AAEa,QAAA,UAAU,GAAG,YAAY,CAAC;AAC1B,QAAA,UAAU,GAAG,YAAY,CAAC;AAC1B,QAAA,YAAY,GAAG,cAAc,CAAC;AAC9B,QAAA,QAAQ,GAAG,UAAU,CAAC;AACtB,QAAA,WAAW,GAAG,aAAa,CAAC;AAE5B,QAAA,WAAW,GAAG,QAAQ,CAAC;AACvB,QAAA,WAAW,GAAG,QAAQ,CAAC;AACvB,QAAA,aAAa,GAAG,QAAQ,CAAC;AACzB,QAAA,SAAS,GAAG,QAAQ,CAAC;AACrB,QAAA,YAAY,GAAG,QAAQ,CAAC;AAExB,QAAA,KAAK,GAAG,OAAO,CAAC;AAChB,QAAA,MAAM,GAAG,QAAQ,CAAC;AAElB,QAAA,WAAW,GAAG,OAAO,CAAC;AAEtB,QAAA,aAAa,GAAa;IACrC,UAAU,EAAE,SAAS;IACrB,UAAU,EAAE,SAAS;IACrB,YAAY,EAAE,SAAS;IACvB,QAAQ,EAAE,SAAS;IACnB,WAAW,EAAE,SAAS;IACtB,QAAQ,EAAE,SAAS;IACnB,gBAAgB,EAAE,SAAS;IAC3B,KAAK,EAAE,SAAS;IAChB,MAAM,EAAE,SAAS;CAClB,CAAC;AAEW,QAAA,IAAI,GAAG,MAAM,CAAC","sourcesContent":["import { Campaign } from '@amplitude/analytics-types';\n\nexport const UTM_SOURCE = 'utm_source';\nexport const UTM_MEDIUM = 'utm_medium';\nexport const UTM_CAMPAIGN = 'utm_campaign';\nexport const UTM_TERM = 'utm_term';\nexport const UTM_CONTENT = 'utm_content';\n\nexport const UTMZ_SOURCE = 'utmcsr';\nexport const UTMZ_MEDIUM = 'utmcmd';\nexport const UTMZ_CAMPAIGN = 'utmccn';\nexport const UTMZ_TERM = 'utmctr';\nexport const UTMZ_CONTENT = 'utmcct';\n\nexport const GCLID = 'gclid';\nexport const FBCLID = 'fbclid';\n\nexport const EMPTY_VALUE = 'EMPTY';\n\nexport const BASE_CAMPAIGN: Campaign = {\n utm_source: undefined,\n utm_medium: undefined,\n utm_campaign: undefined,\n utm_term: undefined,\n utm_content: undefined,\n referrer: undefined,\n referring_domain: undefined,\n gclid: undefined,\n fbclid: undefined,\n};\n\nexport const MKTG = 'MKTG';\n"]}
@@ -1,11 +1,13 @@
1
1
  import { AmplitudeCore } from '@amplitude/analytics-core';
2
- import { BrowserConfig, BrowserOptions, EventOptions, Identify as IIdentify, Result, Revenue as IRevenue, TransportType } from '@amplitude/analytics-types';
2
+ import { AdditionalBrowserOptions, AttributionBrowserOptions, BrowserConfig, BrowserOptions, EventOptions, Identify as IIdentify, Result, Revenue as IRevenue, TransportType } from '@amplitude/analytics-types';
3
3
  export declare class AmplitudeBrowser extends AmplitudeCore<BrowserConfig> {
4
- init(apiKey: string, userId?: string, options?: BrowserOptions): Promise<void>;
4
+ init(apiKey: string, userId?: string, options?: BrowserOptions & AdditionalBrowserOptions): Promise<void>;
5
+ runAttributionStrategy(attributionConfig?: AttributionBrowserOptions, isNewSession?: boolean): Promise<void>;
5
6
  getUserId(): string | undefined;
6
- setUserId(userId: string): void;
7
+ setUserId(userId: string | undefined): void;
7
8
  getDeviceId(): string | undefined;
8
9
  setDeviceId(deviceId: string): void;
10
+ regenerateDeviceId(): void;
9
11
  getSessionId(): number | undefined;
10
12
  setSessionId(sessionId: number): void;
11
13
  setOptOut(optOut: boolean): void;
@@ -13,7 +15,6 @@ export declare class AmplitudeBrowser extends AmplitudeCore<BrowserConfig> {
13
15
  identify(identify: IIdentify, eventOptions?: EventOptions): Promise<Result>;
14
16
  groupIdentify(groupType: string, groupName: string | string[], identify: IIdentify, eventOptions?: EventOptions): Promise<Result>;
15
17
  revenue(revenue: IRevenue, eventOptions?: EventOptions): Promise<Result>;
16
- trackAttributions(): Promise<Result> | undefined;
17
18
  }
18
19
  /**
19
20
  * Initializes the Amplitude SDK with your apiKey, userId and optional configurations.
@@ -23,7 +24,7 @@ export declare class AmplitudeBrowser extends AmplitudeCore<BrowserConfig> {
23
24
  * await init(API_KEY, USER_ID, options).promise;
24
25
  * ```
25
26
  */
26
- export declare const init: (apiKey: string, userId?: string | undefined, options?: BrowserOptions | undefined) => import("@amplitude/analytics-types").AmplitudeReturn<void>;
27
+ export declare const init: (apiKey: string, userId?: string | undefined, options?: (BrowserOptions & AdditionalBrowserOptions) | undefined) => import("@amplitude/analytics-types").AmplitudeReturn<void>;
27
28
  /**
28
29
  * Adds a new plugin.
29
30
  *
@@ -132,7 +133,7 @@ export declare const getUserId: () => string | undefined;
132
133
  * setUserId('userId');
133
134
  * ```
134
135
  */
135
- export declare const setUserId: (userId: string) => void;
136
+ export declare const setUserId: (userId: string | undefined) => void;
136
137
  /**
137
138
  * Returns current device ID.
138
139
  *
@@ -151,6 +152,16 @@ export declare const getDeviceId: () => string | undefined;
151
152
  * ```
152
153
  */
153
154
  export declare const setDeviceId: (deviceId: string) => void;
155
+ /**
156
+ * Regenerates a new random deviceId for current user. Note: this is not recommended unless you know what you
157
+ * are doing. This can be used in conjunction with `setUserId(undefined)` to anonymize users after they log out.
158
+ * With an `unefined` userId and a completely new deviceId, the current user would appear as a brand new user in dashboard.
159
+ *
160
+ * ```typescript
161
+ * regenerateDeviceId();
162
+ * ```
163
+ */
164
+ export declare const regenerateDeviceId: () => void;
154
165
  /**
155
166
  * Returns current session ID.
156
167
  *
@@ -1 +1 @@
1
- {"version":3,"file":"browser-client.d.ts","sourceRoot":"","sources":["../../src/browser-client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAiD,MAAM,2BAA2B,CAAC;AACzG,OAAO,EACL,aAAa,EACb,cAAc,EACd,YAAY,EACZ,QAAQ,IAAI,SAAS,EACrB,MAAM,EACN,OAAO,IAAI,QAAQ,EACnB,aAAa,EACd,MAAM,4BAA4B,CAAC;AAQpC,qBAAa,gBAAiB,SAAQ,aAAa,CAAC,aAAa,CAAC;IAC1D,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,cAAc;IAwBpE,SAAS;IAIT,SAAS,CAAC,MAAM,EAAE,MAAM;IAKxB,WAAW;IAIX,WAAW,CAAC,QAAQ,EAAE,MAAM;IAK5B,YAAY;IAIZ,YAAY,CAAC,SAAS,EAAE,MAAM;IAK9B,SAAS,C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;;;;;;;;;GAaG;AACH,eAAO,MAAM,YAAY,cApPC,aAAa,SAoPqB,CAAC"}
@@ -1,13 +1,12 @@
1
1
  Object.defineProperty(exports, "__esModule", { value: true });
2
- exports.setTransport = exports.setOptOut = exports.setSessionId = exports.getSessionId = exports.setDeviceId = exports.getDeviceId = exports.setUserId = exports.getUserId = exports.revenue = exports.setGroup = exports.groupIdentify = exports.identify = exports.logEvent = exports.track = exports.remove = exports.add = exports.init = exports.AmplitudeBrowser = void 0;
2
+ exports.setTransport = exports.setOptOut = exports.setSessionId = exports.getSessionId = exports.regenerateDeviceId = exports.setDeviceId = exports.getDeviceId = exports.setUserId = exports.getUserId = exports.revenue = exports.setGroup = exports.groupIdentify = exports.identify = exports.logEvent = exports.track = exports.remove = exports.add = exports.init = exports.AmplitudeBrowser = void 0;
3
3
  var tslib_1 = require("tslib");
4
4
  var analytics_core_1 = require("@amplitude/analytics-core");
5
5
  var snippet_helper_1 = require("./utils/snippet-helper");
6
6
  var context_1 = require("./plugins/context");
7
7
  var config_1 = require("./config");
8
- var attribution_1 = require("./attribution");
9
- var session_manager_1 = require("./session-manager");
10
8
  var cookie_migration_1 = require("./cookie-migration");
9
+ var campaign_tracker_1 = require("./attribution/campaign-tracker");
11
10
  var AmplitudeBrowser = /** @class */ (function (_super) {
12
11
  (0, tslib_1.__extends)(AmplitudeBrowser, _super);
13
12
  function AmplitudeBrowser() {
@@ -16,27 +15,58 @@ var AmplitudeBrowser = /** @class */ (function (_super) {
16
15
  AmplitudeBrowser.prototype.init = function (apiKey, userId, options) {
17
16
  var _a, _b, _c;
18
17
  return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
19
- var oldCookies, browserOptions;
18
+ var oldCookies, browserOptions, isNewSession;
20
19
  return (0, tslib_1.__generator)(this, function (_d) {
21
20
  switch (_d.label) {
22
21
  case 0:
23
22
  oldCookies = (0, cookie_migration_1.parseOldCookies)(apiKey, options);
24
- browserOptions = (0, config_1.useBrowserConfig)(apiKey, userId || oldCookies.userId, (0, tslib_1.__assign)((0, tslib_1.__assign)({}, options), { deviceId: (_a = oldCookies.deviceId) !== null && _a !== void 0 ? _a : options === null || options === void 0 ? void 0 : options.deviceId, sessionId: (_b = oldCookies.sessionId) !== null && _b !== void 0 ? _b : options === null || options === void 0 ? void 0 : options.sessionId, optOut: (_c = options === null || options === void 0 ? void 0 : options.optOut) !== null && _c !== void 0 ? _c : oldCookies.optOut }));
23
+ browserOptions = (0, config_1.useBrowserConfig)(apiKey, userId || oldCookies.userId, (0, tslib_1.__assign)((0, tslib_1.__assign)({}, options), { deviceId: (_a = oldCookies.deviceId) !== null && _a !== void 0 ? _a : options === null || options === void 0 ? void 0 : options.deviceId, sessionId: (_b = oldCookies.sessionId) !== null && _b !== void 0 ? _b : options === null || options === void 0 ? void 0 : options.sessionId, optOut: (_c = options === null || options === void 0 ? void 0 : options.optOut) !== null && _c !== void 0 ? _c : oldCookies.optOut, lastEventTime: oldCookies.lastEventTime }));
25
24
  return [4 /*yield*/, _super.prototype.init.call(this, undefined, undefined, browserOptions)];
26
25
  case 1:
27
26
  _d.sent();
28
- // Step 3: Store user session in cookie storage
29
- (0, session_manager_1.updateCookies)(this.config, oldCookies.lastEventTime);
27
+ isNewSession = false;
28
+ if (!this.config.sessionId ||
29
+ (this.config.lastEventTime && Date.now() - this.config.lastEventTime > this.config.sessionTimeout)) {
30
+ // Either
31
+ // 1) No previous session; or
32
+ // 2) Previous session expired
33
+ this.config.sessionId = Date.now();
34
+ isNewSession = true;
35
+ }
30
36
  // Step 4: Install plugins
37
+ // Do not track any events before this
31
38
  return [4 /*yield*/, this.add(new context_1.Context())];
32
39
  case 2:
33
40
  // Step 4: Install plugins
41
+ // Do not track any events before this
34
42
  _d.sent();
35
43
  return [4 /*yield*/, this.add(new analytics_core_1.Destination())];
36
44
  case 3:
37
45
  _d.sent();
38
- // Step 4: Track attributions
39
- void this.trackAttributions();
46
+ // Step 5: Track attributions
47
+ return [4 /*yield*/, this.runAttributionStrategy(options === null || options === void 0 ? void 0 : options.attribution, isNewSession)];
48
+ case 4:
49
+ // Step 5: Track attributions
50
+ _d.sent();
51
+ return [2 /*return*/];
52
+ }
53
+ });
54
+ });
55
+ };
56
+ AmplitudeBrowser.prototype.runAttributionStrategy = function (attributionConfig, isNewSession) {
57
+ if (isNewSession === void 0) { isNewSession = false; }
58
+ return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
59
+ var track, onNewCampaign, storage, campaignTracker;
60
+ return (0, tslib_1.__generator)(this, function (_a) {
61
+ switch (_a.label) {
62
+ case 0:
63
+ track = this.track.bind(this);
64
+ onNewCampaign = this.setSessionId.bind(this, Date.now());
65
+ storage = (0, config_1.createFlexibleStorage)(this.config);
66
+ campaignTracker = new campaign_tracker_1.CampaignTracker(this.config.apiKey, (0, tslib_1.__assign)((0, tslib_1.__assign)({}, attributionConfig), { storage: storage, track: track, onNewCampaign: onNewCampaign }));
67
+ return [4 /*yield*/, campaignTracker.send(isNewSession)];
68
+ case 1:
69
+ _a.sent();
40
70
  return [2 /*return*/];
41
71
  }
42
72
  });
@@ -47,25 +77,25 @@ var AmplitudeBrowser = /** @class */ (function (_super) {
47
77
  };
48
78
  AmplitudeBrowser.prototype.setUserId = function (userId) {
49
79
  this.config.userId = userId;
50
- (0, session_manager_1.updateCookies)(this.config);
51
80
  };
52
81
  AmplitudeBrowser.prototype.getDeviceId = function () {
53
82
  return this.config.deviceId;
54
83
  };
55
84
  AmplitudeBrowser.prototype.setDeviceId = function (deviceId) {
56
85
  this.config.deviceId = deviceId;
57
- (0, session_manager_1.updateCookies)(this.config);
86
+ };
87
+ AmplitudeBrowser.prototype.regenerateDeviceId = function () {
88
+ var deviceId = (0, config_1.createDeviceId)();
89
+ this.setDeviceId(deviceId);
58
90
  };
59
91
  AmplitudeBrowser.prototype.getSessionId = function () {
60
92
  return this.config.sessionId;
61
93
  };
62
94
  AmplitudeBrowser.prototype.setSessionId = function (sessionId) {
63
95
  this.config.sessionId = sessionId;
64
- (0, session_manager_1.updateCookies)(this.config);
65
96
  };
66
97
  AmplitudeBrowser.prototype.setOptOut = function (optOut) {
67
- _super.prototype.setOptOut.call(this, optOut);
68
- (0, session_manager_1.updateCookies)(this.config);
98
+ this.config.optOut = optOut;
69
99
  };
70
100
  AmplitudeBrowser.prototype.setTransport = function (transport) {
71
101
  this.config.transportProvider = (0, config_1.createTransport)(transport);
@@ -94,21 +124,6 @@ var AmplitudeBrowser = /** @class */ (function (_super) {
94
124
  }
95
125
  return _super.prototype.revenue.call(this, revenue, eventOptions);
96
126
  };
97
- AmplitudeBrowser.prototype.trackAttributions = function () {
98
- var attributions = (0, attribution_1.getAttributions)(this.config);
99
- if (Object.keys(attributions).length === 0) {
100
- return;
101
- }
102
- var id = new analytics_core_1.Identify();
103
- Object.entries(attributions).forEach(function (_a) {
104
- var _b = (0, tslib_1.__read)(_a, 2), key = _b[0], value = _b[1];
105
- if (value) {
106
- id.setOnce("initial_".concat(key), value);
107
- id.set(key, value);
108
- }
109
- });
110
- return this.identify(id);
111
- };
112
127
  return AmplitudeBrowser;
113
128
  }(analytics_core_1.AmplitudeCore));
114
129
  exports.AmplitudeBrowser = AmplitudeBrowser;
@@ -249,6 +264,16 @@ exports.getDeviceId = client.getDeviceId.bind(client);
249
264
  * ```
250
265
  */
251
266
  exports.setDeviceId = client.setDeviceId.bind(client);
267
+ /**
268
+ * Regenerates a new random deviceId for current user. Note: this is not recommended unless you know what you
269
+ * are doing. This can be used in conjunction with `setUserId(undefined)` to anonymize users after they log out.
270
+ * With an `unefined` userId and a completely new deviceId, the current user would appear as a brand new user in dashboard.
271
+ *
272
+ * ```typescript
273
+ * regenerateDeviceId();
274
+ * ```
275
+ */
276
+ exports.regenerateDeviceId = client.regenerateDeviceId.bind(client);
252
277
  /**
253
278
  * Returns current session ID.
254
279
  *