Skip to content
This repository has been archived by the owner on Nov 23, 2019. It is now read-only.

support strict utm parsing #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var Segment = exports = module.exports = integration('Segment.io')
.option('crossDomainIdServers', [])
.option('retryQueue', false)
.option('addBundledMetadata', false)
.option('strictUTM', false)
.option('unbundledIntegrations', []);

/**
Expand Down Expand Up @@ -216,6 +217,10 @@ Segment.prototype.normalize = function(msg) {
ctx.traits.crossDomainId = crossDomainId;
}
}

// only use strict parsing on new / deliberately enabled
utm = this.options.strictUTM ? utm.strict : utm;

// if user provides campaign via context, do not overwrite with UTM qs param
if (query && !ctx.campaign) {
ctx.campaign = utm(query);
Expand Down
22 changes: 21 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ describe('Segment.io', function() {

it('should add .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';
Segment.global.location.search = '?utm_source=source&utm_medium=medium&utm_term=term&utm_content=content&utm_campaign=name&utm_test=val';
Segment.global.location.hostname = 'localhost';
segment.normalize(object);
analytics.assert(object);
Expand All @@ -225,6 +225,7 @@ describe('Segment.io', function() {
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 === 'val');
Segment.global = window;
});

Expand Down Expand Up @@ -255,6 +256,25 @@ describe('Segment.io', function() {
Segment.global = window;
});

it('should add only specced fields to .campaign if strictmode is enabled', function() {
segment.options.strictUTM = true;
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';
Expand Down