@@ -578,6 +578,9 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
578578 if ( this . options . includeReferrer ) {
579579 this . _saveReferrer ( this . _getReferrer ( ) ) ;
580580 }
581+ if ( this . options . includeGclid ) {
582+ this . _saveGclid ( ) ;
583+ }
581584 }
582585 this . _lastEventTime = now ;
583586 _saveCookieData ( this ) ;
@@ -960,6 +963,21 @@ AmplitudeClient.prototype._getReferrer = function _getReferrer() {
960963 return document . referrer ;
961964} ;
962965
966+ /**
967+ * Try to fetch Google Gclid from url params.
968+ * @private
969+ */
970+ AmplitudeClient . prototype . _saveGclid = function _saveGclid ( queryParams ) {
971+ queryParams = queryParams || location . search ;
972+ debugger ;
973+ var gclid = utils . getQueryParam ( 'gclid' , queryParams ) ;
974+ if ( utils . isEmptyString ( gclid ) ) {
975+ return ;
976+ }
977+ var gclidProperties = { 'gclid' : gclid } ;
978+ _sendUserPropertiesOncePerSession ( this , Constants . GCLID , gclidProperties ) ;
979+ } ;
980+
963981/**
964982 * Parse the domain from referrer info
965983 * @private
@@ -1635,6 +1653,7 @@ module.exports = {
16351653 REFERRER : 'amplitude_referrer' ,
16361654 SESSION_ID : 'amplitude_sessionId' ,
16371655 UTM_PROPERTIES : 'amplitude_utm_properties' ,
1656+ GCLID : 'amplitude_gclid' ,
16381657
16391658 // Used in cookie as well
16401659 DEVICE_ID : 'amplitude_deviceId' ,
@@ -2561,12 +2580,12 @@ var regexp = /[a-z0-9][a-z0-9\-]*[a-z0-9]\.[a-z\.]{2,6}$/i;
25612580
25622581/**
25632582 * Get the top domain.
2564- *
2583+ *
25652584 * Official Grammar: http://tools.ietf.org/html/rfc883#page-56
25662585 * Look for tlds with up to 2-6 characters.
2567- *
2586+ *
25682587 * Example:
2569- *
2588+ *
25702589 * domain('http://localhost:3000/baz');
25712590 * // => ''
25722591 * domain('http://dev:3000/baz');
@@ -2575,7 +2594,7 @@ var regexp = /[a-z0-9][a-z0-9\-]*[a-z0-9]\.[a-z\.]{2,6}$/i;
25752594 * // => ''
25762595 * domain('http://segment.io/baz');
25772596 * // => 'segment.io'
2578- *
2597+ *
25792598 * @param {String } url
25802599 * @return {String }
25812600 * @api public
@@ -2864,9 +2883,18 @@ var validateGroupName = function validateGroupName(key, groupName) {
28642883 '. Please use strings or array of strings for groupName' ) ;
28652884} ;
28662885
2886+ // parses the value of a url param (for example ?gclid=1234&...)
2887+ var getQueryParam = function getQueryParam ( name , query ) {
2888+ name = name . replace ( / [ \[ ] / , "\\[" ) . replace ( / [ \] ] / , "\\]" ) ;
2889+ var regex = new RegExp ( "[\\?&]" + name + "=([^&#]*)" ) ;
2890+ var results = regex . exec ( query ) ;
2891+ return results === null ? undefined : decodeURIComponent ( results [ 1 ] . replace ( / \+ / g, " " ) ) ;
2892+ } ;
2893+
28672894module . exports = {
28682895 log : log ,
28692896 isEmptyString : isEmptyString ,
2897+ getQueryParam : getQueryParam ,
28702898 sessionStorageEnabled : sessionStorageEnabled ,
28712899 truncate : truncate ,
28722900 validateGroups : validateGroups ,
@@ -3029,20 +3057,13 @@ module.exports = localStorage;
3029305713 : [ function ( require , module , exports ) {
30303058var utils = require ( './utils' ) ;
30313059
3032- var getUtmParam = function getUtmParam ( name , query ) {
3033- name = name . replace ( / [ \[ ] / , "\\[" ) . replace ( / [ \] ] / , "\\]" ) ;
3034- var regex = new RegExp ( "[\\?&]" + name + "=([^&#]*)" ) ;
3035- var results = regex . exec ( query ) ;
3036- return results === null ? undefined : decodeURIComponent ( results [ 1 ] . replace ( / \+ / g, " " ) ) ;
3037- } ;
3038-
30393060var getUtmData = function getUtmData ( rawCookie , query ) {
30403061 // Translate the utmz cookie format into url query string format.
30413062 var cookie = rawCookie ? '?' + rawCookie . split ( '.' ) . slice ( - 1 ) [ 0 ] . replace ( / \| / g, '&' ) : '' ;
30423063
30433064 var fetchParam = function fetchParam ( queryName , query , cookieName , cookie ) {
3044- return getUtmParam ( queryName , query ) ||
3045- getUtmParam ( cookieName , cookie ) ;
3065+ return utils . getQueryParam ( queryName , query ) ||
3066+ utils . getQueryParam ( cookieName , cookie ) ;
30463067 } ;
30473068
30483069 var utmSource = fetchParam ( 'utm_source' , query , 'utmcsr' , cookie ) ;
@@ -3267,7 +3288,7 @@ module.exports = Identify;
32673288 *
32683289 * Licensed under the MIT license:
32693290 * http://www.opensource.org/licenses/MIT
3270- *
3291+ *
32713292 * Based on
32723293 * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
32733294 * Digest Algorithm, as defined in RFC 1321.
@@ -4950,6 +4971,7 @@ module.exports = {
49504971 eventUploadThreshold : 30 ,
49514972 eventUploadPeriodMillis : 30 * 1000 , // 30s
49524973 forceHttps : false ,
4974+ includeGclid : false ,
49534975} ;
49544976
49554977} , { "./language" :29 } ] ,
@@ -4964,4 +4986,4 @@ module.exports = {
49644986} ;
49654987
49664988} , { } ] } , { } , { "1" :"" } )
4967- ) ;
4989+ ) ;
0 commit comments