From f4cada44bfa1f43a64059d1e15ece2c696400c76 Mon Sep 17 00:00:00 2001 From: Chris Sperandio Date: Thu, 27 Aug 2015 00:53:41 -0700 Subject: [PATCH] use strict utm parser, only populate the 5 specced context.campaign fields --- component.json | 2 +- lib/index.js | 2 +- test/index.test.js | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/component.json b/component.json index 065606e..b516385 100644 --- a/component.json +++ b/component.json @@ -15,7 +15,7 @@ "segmentio/json": "1.0.0", "segmentio/protocol": "0.0.2", "segmentio/top-domain": "1.0.0", - "segmentio/utm-params": "1.0.2", + "segmentio/utm-params": "1.1.0", "yields/send-json": "1.1.1", "yields/store": "1.0.2" }, diff --git a/lib/index.js b/lib/index.js index be26e35..e33f30f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -164,7 +164,7 @@ Segment.prototype.normalize = function(msg) { msg.writeKey = this.options.apiKey; ctx.userAgent = navigator.userAgent; if (!ctx.library) ctx.library = { name: 'analytics.js', version: this.analytics.VERSION }; - if (query) ctx.campaign = utm(query); + if (query) ctx.campaign = utm.strict(query); this.referrerId(query, ctx); msg.userId = msg.userId || user.id(); msg.anonymousId = user.anonymousId(); diff --git a/test/index.test.js b/test/index.test.js index da34851..8f543a0 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -204,6 +204,24 @@ describe('Segment.io', function() { Segment.global = window; }); + it('should add only specced fields to .campaign', function() { + Segment.global = { navigator: {}, location: {} }; + Segment.global.location.search = '?utm_source=source&utm_medium=medium&utm_term=term&utm_content=content&utm_campaign=name&utm_test=test&utm_fake=fake'; + Segment.global.location.hostname = 'localhost'; + segment.normalize(object); + analytics.assert(object); + analytics.assert(object.context); + analytics.assert(object.context.campaign); + analytics.assert(object.context.campaign.source === 'source'); + analytics.assert(object.context.campaign.medium === 'medium'); + analytics.assert(object.context.campaign.term === 'term'); + analytics.assert(object.context.campaign.content === 'content'); + analytics.assert(object.context.campaign.name === 'name'); + analytics.assert(object.context.campaign.test === undefined); + analytics.assert(object.context.campaign.fake === undefined); + Segment.global = window; + }); + it('should add .referrer.id and .referrer.type', function() { Segment.global = { navigator: {}, location: {} }; Segment.global.location.search = '?utm_source=source&urid=medium';