-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathset-start-anchors.js
76 lines (62 loc) · 1.85 KB
/
set-start-anchors.js
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
72
73
74
75
76
import { getParameterByName, getCookie } from '../theme/functions';
/**
* This file builds a query string to attach to anchor links so that we can track data on our other domains.
*/
( function( $ ) {
const starterQueryString = getStarterString();
$( 'a' ).prop( 'href', function() {
const href = $( this ).attr( 'href' );
const hrefUrl = this.href;
if ( href.match( /^(#|mailto|tel)/ ) ) {
return href;
} else if ( hrefUrl.indexOf( '?' ) >= 0 && hrefUrl.indexOf( '#' ) === -1 ) {
return hrefUrl + '&' + starterQueryString;
} else if ( hrefUrl.indexOf( '#' ) >= 0 ) {
return hrefUrl;
} else if ( hrefUrl.indexOf( '?' ) === -1 && hrefUrl.indexOf( '#' ) === -1 ) {
return hrefUrl + '?' + starterQueryString;
}
} );
} )( jQuery );
function getStarterString() {
const starter = window.location.hostname;
let string = '';
const parameters = [];
const trackersToUse = [
'utm_source',
'utm_medium',
'utm_term',
'utm_content',
'utm_campaign',
'track',
];
trackersToUse.forEach( function( tracker ) {
parameters.push( {
key: tracker,
value: getParameterByName( tracker ),
} );
} );
const filteredParams = parameters.filter( parameter => parameter.value );
if ( filteredParams.length ) {
string = 'start=' + starter;
filteredParams.forEach( function( parameterObject ) {
string += '&' + parameterObject.key + '=' + parameterObject.value;
} );
} else {
const cookies = [];
trackersToUse.forEach( function( tracker ) {
cookies.push( {
key: tracker,
value: getCookie( tracker + '1' ),
} );
} );
const filteredCookies = cookies.filter( parameter => parameter.value );
if ( filteredCookies.length ) {
string = 'start=' + starter;
filteredCookies.forEach( function( parameterObject ) {
string += '&' + parameterObject.key + '=' + parameterObject.value;
} );
}
}
return string;
}