swetrix 2.3.1 → 3.0.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.
package/src/utils.ts CHANGED
@@ -1,3 +1,8 @@
1
+ interface IGetPath {
2
+ hash?: boolean
3
+ search?: boolean
4
+ }
5
+
1
6
  const findInSearch = (exp: RegExp): string | undefined => {
2
7
  const res = location.search.match(exp)
3
8
  return (res && res[2]) || undefined
@@ -41,7 +46,34 @@ export const getUTMMedium = () => findInSearch(utmMediumRegex)
41
46
 
42
47
  export const getUTMCampaign = () => findInSearch(utmCampaignRegex)
43
48
 
44
- export const getPath = (): string => {
45
- // TODO: Maybe we should also include such data as location.hash or location.search
46
- return location.pathname || ''
49
+ /**
50
+ * Function used to track the current page (path) of the application.
51
+ * Will work in cases where the path looks like:
52
+ * - /path
53
+ * - /#/path
54
+ * - /path?search
55
+ * - /path?search#hash
56
+ * - /path#hash?search
57
+ *
58
+ * @param options - Options for the function.
59
+ * @param options.hash - Whether to trigger on hash change.
60
+ * @param options.search - Whether to trigger on search change.
61
+ * @returns The path of the current page.
62
+ */
63
+ export const getPath = (options: IGetPath): string => {
64
+ let result = location.pathname || ''
65
+
66
+ if (options.hash) {
67
+ const hashIndex = location.hash.indexOf('?')
68
+ const hashString = hashIndex > -1 ? location.hash.substring(0, hashIndex) : location.hash
69
+ result += hashString
70
+ }
71
+
72
+ if (options.search) {
73
+ const hashIndex = location.hash.indexOf('?')
74
+ const searchString = location.search || (hashIndex > -1 ? location.hash.substring(hashIndex) : '')
75
+ result += searchString
76
+ }
77
+
78
+ return result
47
79
  }