@thumbmarkjs/thumbmarkjs 1.5.1 → 1.6.1

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.
@@ -1,5 +1,5 @@
1
1
  import { getVisitorId, setVisitorId } from './visitorId';
2
- import { optionsInterface, defaultOptions } from '../options';
2
+ import { defaultOptions, options, OptionsAfterDefaults } from '../options';
3
3
 
4
4
  describe('visitorId storage tests', () => {
5
5
  beforeEach(() => {
@@ -21,7 +21,7 @@ describe('visitorId storage tests', () => {
21
21
 
22
22
  test('should use custom storage property name', () => {
23
23
  const visitorId = 'custom-visitor-456';
24
- const customOptions: optionsInterface = {
24
+ const customOptions: OptionsAfterDefaults = {
25
25
  ...defaultOptions,
26
26
  storage_property_name: 'my_custom_visitor_key'
27
27
  };
@@ -37,7 +37,7 @@ describe('visitorId storage tests', () => {
37
37
  });
38
38
 
39
39
  test('should return null when storage property does not exist', () => {
40
- const options: optionsInterface = {
40
+ const options: OptionsAfterDefaults = {
41
41
  ...defaultOptions,
42
42
  storage_property_name: 'nonexistent_key'
43
43
  };
@@ -56,6 +56,15 @@ describe('visitorId storage tests', () => {
56
56
  setVisitorId(newVisitorId, options);
57
57
  expect(getVisitorId(options)).toBe(newVisitorId);
58
58
  });
59
+
60
+ test('should migrate from old value in case new prefix is set', () => {
61
+ const visitorId = 'test-visitor-123';
62
+ setVisitorId(visitorId, options);
63
+ expect(getVisitorId({
64
+ property_name_factory: (name) => `custom_prefix_${name}`,
65
+ } as OptionsAfterDefaults)).toBe(visitorId);
66
+ expect(localStorage.getItem(`custom_prefix_visitor_id`)).toBe(visitorId);
67
+ })
59
68
  });
60
69
 
61
70
  describe('error handling', () => {
@@ -1,18 +1,42 @@
1
- import { optionsInterface } from "../options";
1
+ import { DEFAULT_STORAGE_PREFIX, OptionsAfterDefaults } from "../options";
2
2
 
3
3
  /**
4
4
  * Visitor ID storage utilities - localStorage only, server generates IDs
5
5
  */
6
6
 
7
- const DEFAULT_STORAGE_PROPERTY_NAME = 'thumbmark_visitor_id';
7
+ const DEFAULT_STORAGE_PROPERTY_NAME = 'visitor_id';
8
8
 
9
+ /**
10
+ * Get the storage property name for visitor id
11
+ * @param _options
12
+ */
13
+ export function getVisitorIdPropertyName(
14
+ _options: Pick<OptionsAfterDefaults, 'storage_property_name' | 'property_name_factory'>
15
+ ): string {
16
+ if(_options.storage_property_name) {
17
+ return _options.storage_property_name;
18
+ }
19
+
20
+ return _options.property_name_factory(DEFAULT_STORAGE_PROPERTY_NAME);
21
+ }
22
+
23
+ const DEFAULT_VISITOR_ID_NAME = `${DEFAULT_STORAGE_PREFIX}_${DEFAULT_STORAGE_PROPERTY_NAME}`;
9
24
  /**
10
25
  * Gets visitor ID from localStorage, returns null if unavailable
11
26
  */
12
- export function getVisitorId(_options: optionsInterface): string | null {
13
- const storagePropertyName = _options.storage_property_name || DEFAULT_STORAGE_PROPERTY_NAME;
27
+ export function getVisitorId(_options: OptionsAfterDefaults): string | null {
14
28
  try {
15
- return localStorage.getItem(storagePropertyName);
29
+ const propertyName = getVisitorIdPropertyName(_options);
30
+ let visitorId = localStorage.getItem(propertyName);
31
+ if(!visitorId && propertyName !== DEFAULT_VISITOR_ID_NAME) {
32
+ // Migration case in case going from thumbmark prefix to a custom one
33
+ visitorId = localStorage.getItem(DEFAULT_VISITOR_ID_NAME);
34
+ if(visitorId) {
35
+ setVisitorId(visitorId, _options);
36
+ }
37
+ }
38
+
39
+ return visitorId;
16
40
  } catch {
17
41
  return null;
18
42
  }
@@ -21,10 +45,9 @@ export function getVisitorId(_options: optionsInterface): string | null {
21
45
  /**
22
46
  * Sets visitor ID in localStorage
23
47
  */
24
- export function setVisitorId(visitorId: string, _options: optionsInterface): void {
25
- const storagePropertyName = _options.storage_property_name || DEFAULT_STORAGE_PROPERTY_NAME;
48
+ export function setVisitorId(visitorId: string, _options: OptionsAfterDefaults): void {
26
49
  try {
27
- localStorage.setItem(storagePropertyName, visitorId);
50
+ localStorage.setItem(getVisitorIdPropertyName(_options), visitorId);
28
51
  } catch {
29
52
  // Ignore storage errors
30
53
  }