Skip to content

Commit

Permalink
Merge tag 'adwords-npe' into develop
Browse files Browse the repository at this point in the history
Fix for location's parameter '&gclid' or '&gclsrc' without '?'.
  • Loading branch information
Cyril committed Oct 16, 2023
2 parents 6346bff + a20186c commit fa589fd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,9 @@ public void init(final ProcessContext context) throws InitializationException {
private boolean adwords(final String location, final SourceOfTrafficMap sourceOfTraffic) {
boolean processed = false;

final boolean adwords = location!=null && ADWORDS_RE.matcher(location).matches();
if (adwords || (location!=null && DOUBLECLICK_RE.matcher(location).matches())) {
final boolean hasQuery = location!=null && location.contains("?");
final boolean adwords = hasQuery && ADWORDS_RE.matcher(location).matches();
if (adwords || (hasQuery && DOUBLECLICK_RE.matcher(location).matches())) {
processed = true;
sourceOfTraffic.setSource(GOOGLE);
sourceOfTraffic.setMedium(CPC);
Expand All @@ -346,11 +347,17 @@ private boolean adwords(final String location, final SourceOfTrafficMap sourceOf
sourceOfTraffic.setKeyword(adwords ? ADWORDS : DOUBLECLICK);

try {
setAdwordsCampaign(new URI(location).getQuery().split("&"), sourceOfTraffic);
final String query = new URI(location).getQuery();
if ( query!=null ) {
setAdwordsCampaign(query.split("&"), sourceOfTraffic);
}
} catch (URISyntaxException e) {
// Symbol `|` generates exception in URI but not in URL.
try {
setAdwordsCampaign(new URL(location).getQuery().split("&"), sourceOfTraffic);
final String query = new URL(location).getQuery();
if ( query!=null ) {
setAdwordsCampaign(query.split("&"), sourceOfTraffic);
}
} catch (final MalformedURLException e2) {
getLogger().error("URISyntaxException", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,37 @@ public void testAdwords(final String url) throws InitializationException {
out.assertFieldEquals("source_of_traffic_keyword", "adwords");
}

@Test
public void testGoogleAdNPEFix() throws InitializationException {
// The '?' is missing in the Url before the parameters. This was raising NPE.
final Record record1 = new StandardRecord();
record1.setField("firstVisitedPage", FieldType.STRING, "https://www.xyz_website.com/fr/index.htmlfake&gclid=XXX");
final Record record2 = new StandardRecord();
record2.setField("firstVisitedPage", FieldType.STRING, "https://www.xyz_website.com/fr/index.htmlfake&gclsrc=XXX");

final TestRunner testRunner = getTestRunner();

testRunner.assertValid();
testRunner.setProperty("cache.size", "5");
testRunner.setProperty("debug", "true");
testRunner.setProperty("source.out.field", "source_of_traffic");
testRunner.setProperty("source_of_traffic.hierarchical", "false");
testRunner.enqueue(record1, record2);
testRunner.run();
testRunner.assertAllInputRecordsProcessed();
testRunner.assertOutputRecordsCount(2);

final MockRecord out1 = testRunner.getOutputRecords().get(0);
out1.assertFieldEquals("source_of_traffic_source", "direct");
out1.assertFieldEquals("source_of_traffic_medium", "");
out1.assertFieldEquals("source_of_traffic_campaign", "direct");

final MockRecord out2 = testRunner.getOutputRecords().get(0);
out2.assertFieldEquals("source_of_traffic_source", "direct");
out2.assertFieldEquals("source_of_traffic_medium", "");
out2.assertFieldEquals("source_of_traffic_campaign", "direct");
}

@Test
public void testDoubleClick() throws InitializationException {
// Test the DoubleClick case with presence of gclsrc parameter.
Expand Down

0 comments on commit fa589fd

Please sign in to comment.