File size: 2,219 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import debug from './debug';

/**
 * Decodes a url-safe base64 encoded string.
 * @param {string} str The url-safe base64 encoded string
 * @returns {string} The decoded string
 */
function urlSafeBase64DecodeString( str ) {
	const decodeMap = {
		'-': '+',
		_: '/',
		'.': '=',
	};

	return window.atob( str.replace( /[-_.]/g, ( ch ) => decodeMap[ ch ] ) );
}

/**
 * Decodes a URL param encoded by AMP's linker.js
 * See also https://github.com/ampproject/amphtml/blob/HEAD/extensions/amp-analytics/linker-id-receiving.md
 * @param {string} value Value to be decoded
 * @returns {null | Object} null or and object containing key/value pairs
 */
function parseAmpEncodedParams( value ) {
	value = value
		.split( '*' )
		.filter( ( val ) => val.length )
		.slice( 2 );
	// return null if empty or we have an odd number of elements
	if ( 0 === value.length || 0 !== value.length % 2 ) {
		return null;
	}
	const keyValMap = {};
	for ( let i = 0; i < value.length; i += 2 ) {
		keyValMap[ value[ i ] ] = urlSafeBase64DecodeString( value[ i + 1 ] );
	}

	return keyValMap;
}

/**
 * Returns a URL instance with the original data plus the data extracted from `tk_amp`. Null if not a valid absolute URL.
 * URL parameters explicitly present in the URL take precedence over the ones extracted from `tk_amp`.
 * This function is used to support AMP-compatible tracking.
 * @param {string} url URL to be parsed like `document.location.href`.
 * @returns {Object} A URL instance with the original data plus the data extracted from `tk_amp`. Null if not a valid absolute URL.
 */
export default function urlParseAmpCompatible( url ) {
	try {
		const parsedUrl = new URL( url );

		debug( 'urlParseAmpCompatible: original query:', parsedUrl.search );

		if ( parsedUrl.searchParams.has( 'tk_amp' ) ) {
			const tk_amp = parseAmpEncodedParams( parsedUrl.searchParams.get( 'tk_amp' ) );
			debug( 'urlParseAmpCompatible: tk_amp:', tk_amp );
			for ( const key of Object.keys( tk_amp ) ) {
				if ( ! parsedUrl.searchParams.has( key ) ) {
					parsedUrl.searchParams.set( key, tk_amp[ key ] );
				}
			}
		}

		debug( 'urlParseAmpCompatible: merged query:', parsedUrl.search );

		return parsedUrl;
	} catch {
		return null;
	}
}