pat310 / google-trends-api

An API layer on top of google trends
https://www.npmjs.com/package/google-trends-api
MIT License
889 stars 176 forks source link

Search for the same keyword over multiple locations. #116

Closed ryanbillings closed 5 years ago

ryanbillings commented 5 years ago

This adds support for a single keyword string with an array of geo locations.

I ran into error SyntaxError: Unexpected token C in JSON at position 0 when running a query such as:

googleTrends.interestOverTime({
  keyword: 'javascript',
  startTime: new Date('2018-01-01'),
  geo: ['US-MA', 'US-VA']
});

My solution to this was to search for the same keyword for all regions in the case when keyword is a string while geo is an array.

@pat310 Let me know if you think this is a good change, thanks!

coveralls commented 5 years ago

Coverage Status

Coverage increased (+0.3%) to 97.987% when pulling ca0cec4a847e62aded0d872ef311786891915e47 on ryanbillings:multiGeo-singleKeyword into 58d97b5edbdc3cc883b2d89b61edc45175696a41 on pat310:master.

pat310 commented 5 years ago

Looks good, just one comment! @ryanbillings

pat310 commented 5 years ago

@ryanbillings How will this work if keyword is an array and geo is an array? Will it run every possible combination of keyword and geo?

ryanbillings commented 5 years ago

Thanks for the PR review @pat310 !

The current implementation will run through both the geo and keyword arrays and run a query for each index in the array. If the arrays are mismatching lengths, we get an error (the one I posted above). To address this, I added in a validation in https://github.com/pat310/google-trends-api/pull/116/commits/30662b83578a92d3d923a1c720f6e99f93e60cd2 to make sure the arrays are the same size, along with some README updates to clarify this:

Passing `geo: ['US-CA, US-VA'], keyword: ['wine', 'peanuts']` will search for wine in California and peanuts in Virginia.

Also moved all of the cases for creating errors into a validateObj function which gets called at the beginning of constructObj (needed to satisfy a codeclimate failure 🙂 )