@tryghost/referrer-parser 0.0.2

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2013-2025 Ghost Foundation
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,107 @@
1
+ # Referrer Parser
2
+
3
+ A simple library for parsing referrer URLs to identify their source and medium.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @tryghost/referrer-parser
9
+ # or
10
+ yarn add @tryghost/referrer-parser
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ### Basic Usage
16
+
17
+ ```javascript
18
+ const { parse } = require('@tryghost/referrer-parser');
19
+
20
+ // Parse a referrer URL
21
+ const result = parse('https://www.google.com/search?q=ghost+cms');
22
+ console.log(result);
23
+ // {
24
+ // referrerSource: 'Google',
25
+ // referrerMedium: 'search',
26
+ // referrerUrl: 'www.google.com'
27
+ // }
28
+ ```
29
+
30
+ ### With Site Configuration
31
+
32
+ ```javascript
33
+ const { parse } = require('@tryghost/referrer-parser');
34
+
35
+ // Parse with site configuration to detect internal traffic
36
+ const result = parse(
37
+ 'https://example.com/blog?utm_source=newsletter&utm_medium=email',
38
+ {
39
+ siteUrl: 'https://example.com',
40
+ adminUrl: 'https://example.com/ghost'
41
+ }
42
+ );
43
+ ```
44
+
45
+ ### Using the ReferrerParser Class
46
+
47
+ For more advanced usage, you can use the `ReferrerParser` class directly:
48
+
49
+ ```javascript
50
+ const { ReferrerParser } = require('@tryghost/referrer-parser');
51
+
52
+ // Create a parser instance
53
+ const parser = new ReferrerParser({
54
+ siteUrl: 'https://example.com',
55
+ adminUrl: 'https://example.com/ghost'
56
+ });
57
+
58
+ // Parse multiple URLs with the same configuration
59
+ const result1 = parser.parse('https://www.google.com/search?q=ghost+cms');
60
+ const result2 = parser.parse('https://twitter.com/ghostcms');
61
+ ```
62
+
63
+ ## Features
64
+
65
+ - Identifies sources and mediums from known referrers
66
+ - Handles special cases for Ghost Explore and Ghost Newsletters
67
+ - Detects UTM parameters
68
+ - Works with or without site/admin URL configuration
69
+ - TypeScript support
70
+
71
+ ## TypeScript Support
72
+
73
+ This package is fully written in TypeScript and provides its own type definitions.
74
+
75
+ ```typescript
76
+ import { parse, ReferrerParser, ReferrerData, ParserOptions } from '@tryghost/referrer-parser';
77
+
78
+ // Parse a referrer URL with type safety
79
+ const result: ReferrerData = parse('https://www.google.com/search?q=ghost+cms');
80
+ console.log(result.referrerSource); // 'Google'
81
+ console.log(result.referrerMedium); // 'search'
82
+ console.log(result.referrerUrl); // 'www.google.com'
83
+
84
+ // Configure the parser with typed options
85
+ const options: ParserOptions = {
86
+ siteUrl: 'https://example.com',
87
+ adminUrl: 'https://example.com/ghost'
88
+ };
89
+
90
+ // Create a parser instance with TypeScript
91
+ const parser = new ReferrerParser(options);
92
+ const customResult = parser.parse('https://example.com/blog?utm_source=newsletter');
93
+ ```
94
+
95
+ ## License
96
+
97
+ MIT
98
+
99
+ ## Testing
100
+
101
+ Run the tests with:
102
+
103
+ ```bash
104
+ yarn test
105
+ ```
106
+
107
+ This will run the test suite using Mocha and report on test coverage.