Skip to content

Commit

Permalink
Get the current timezone, so the Google API will return the correct c…
Browse files Browse the repository at this point in the history
…orresponding times in the formatted time fields.
  • Loading branch information
jtalen committed Jan 12, 2018
1 parent 6aa3893 commit 399e182
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -30,6 +30,7 @@ An object with the following options keys:
* **endTime** End of time period of interest (`new Date()` object). If `endTime` is not provided, the current date is selected.
* **geo** Location of interest (`string`).
* **hl** Preferred language (`string` [defaults to english](https://sites.google.com/site/tomihasa/google-language-codes))
* **timezone** Timezone (`number` defaults to the time zone difference, in minutes, from UTC to current locale (host system settings))
* **category** Category to search within (`number` defaults to [all categories](https://github.com/pat310/google-trends-api/wiki/Google-Trends-Categories))
* **resolution** Granularity of the geo search (enumerated `string` ['COUNTRY', 'REGION', 'CITY', 'DMA']). `resolution` is specific to the [interestByRegion](#interestByRegion) method.
* **granularTimeResolution** Boolean that dictates if the results should be given in a finer time resolution (if `startTime` and `endTime` is less than one day, this should be set to `true`)
Expand Down Expand Up @@ -184,6 +185,7 @@ Requires an `object` as the first parameter with the following keys:
* `endTime` - *optional* - type `Date` object - the end of the time range of interest (defaults to `new Date(Date.now())` if not supplied)
* `geo` - *optional* - type `string` - geocode for a country, region, or DMA depending on the granularity required (defaults to worldwide). For example, `geo: 'US-CA-800'` will target the Bakersfield, California, United States or `geo: 'US'` will just target the US.
* `hl` - *optional* - type `string` - preferred language code for results (defaults to english)
* `timezone` - *optional* - type `number` - preferred timezone (defaults to the time zone difference, in minutes, from UTC to current locale (host system settings))
* `category` - *optional* - type `number` - a number corresponding to a particular category to query within (defaults to all categories), see the [category wiki](https://github.com/pat310/google-trends-api/wiki/Google-Trends-Categories) for a complete list
* `granularTimeResolution` - *optional* - type `boolean` - if `true`, will try to return results to a finer time resolution (only relevant for `startTime` and `endTime` less than one week)

Expand Down Expand Up @@ -247,6 +249,8 @@ Requires an `object` as the first parameter with the following keys:
* `geo` - *optional* - type `string` - geocode for a country, region, or DMA depending on the granularity required (defaults to worldwide). For example, `geo: 'US-CA-800'` will target the Bakersfield, California, United States or `geo: 'US'` will just target the US.
* `resolution` - *optional* - type `enumerated string` either `COUNTRY`, `REGION`, `CITY` or `DMA`. Resolution is selected by default otherwise. Trying to select a resolution larger than a specified `geo` will return an error.
* `hl` - *optional* - type `string` - preferred language code for results (defaults to english)
for results (defaults to english)
* `timezone` - *optional* - type `number` - preferred timezone (defaults to the time zone difference, in minutes, from UTC to current locale (host system settings))
* `category` - *optional* - type `number` - a number corresponding to a particular category to query within (defaults to all categories), see the [category wiki](https://github.com/pat310/google-trends-api/wiki/Google-Trends-Categories) for a complete list

Optional callback `function` as the second parameter (otherwise returns a promise)
Expand Down Expand Up @@ -309,6 +313,8 @@ Requires an `object` as the first parameter with the following keys:
* `endTime` - *optional* - type `Date` object - the end of the time range of interest (defaults to `new Date(Date.now())` if not supplied)
* `geo` - *optional* - type `string` - geocode for a country, region, or DMA depending on the granularity required (defaults to worldwide). For example, `geo: 'US-CA-800'` will target the Bakersfield, California, United States or `geo: 'US'` will just target the US.
* `hl` - *optional* - type `string` - preferred language code for results (defaults to english)
for results (defaults to english)
* `timezone` - *optional* - type `number` - preferred timezone (defaults to the time zone difference, in minutes, from UTC to current locale (host system settings))
* `category` - *optional* - type `number` - a number corresponding to a particular category to query within (defaults to all categories), see the [category wiki](https://github.com/pat310/google-trends-api/wiki/Google-Trends-Categories) for a complete list

Optional callback `function` as the second parameter (otherwise returns a promise)
Expand Down Expand Up @@ -352,6 +358,8 @@ Requires an `object` as the first parameter with the following keys:
* `endTime` - *optional* - type `Date` object - the end of the time range of interest (defaults to `new Date(Date.now())` if not supplied)
* `geo` - *optional* - type `string` - geocode for a country, region, or DMA depending on the granularity required (defaults to worldwide). For example, `geo: 'US-CA-800'` will target the Bakersfield, California, United States or `geo: 'US'` will just target the US.
* `hl` - *optional* - type `string` - preferred language code for results (defaults to english)
for results (defaults to english)
* `timezone` - *optional* - type `number` - preferred timezone (defaults to the time zone difference, in minutes, from UTC to current locale (host system settings))
* `category` - *optional* - type `number` - a number corresponding to a particular category to query within (defaults to all categories), see the [category wiki](https://github.com/pat310/google-trends-api/wiki/Google-Trends-Categories) for a complete list

Optional callback `function` as the second parameter (otherwise returns a promise)
Expand Down
10 changes: 10 additions & 0 deletions examples.js
Expand Up @@ -35,6 +35,16 @@
// else console.log(results);
// });

/* ****** Interest over time - Set a custom timezone ***************/

// googleTrends.interestOverTime({
// keyword: 'Valentines Day',
// timezone: new Date().getTimezoneOffset() / 60,
// }, function(err, results) {
// if (err) console.log('oh no error!', err);
// else console.log(results);
// });

/* ****** Interest over time - Comparing multiple keywords *********/
// googleTrends.interestOverTime({keyword: ['Valentines Day', 'Christmas Day']})
// .then((res) => {
Expand Down
5 changes: 3 additions & 2 deletions src/utilities.js
Expand Up @@ -61,6 +61,7 @@ export function constructObj(obj, cbFunc) {

if (!obj.hl) obj.hl = 'en-US';
if (!obj.category) obj.category = 0;
if (!obj.timezone) obj.timezone = new Date().getTimezoneOffset();

if (!cbFunc) {
cbFunc = (err, res) => {
Expand Down Expand Up @@ -165,7 +166,7 @@ export function getResults(request) {
category: obj.category,
property: '',
}),
tz: 300,
tz: obj.timezone,
},
};

Expand Down Expand Up @@ -210,7 +211,7 @@ export function getResults(request) {
hl: obj.hl,
req,
token,
tz: 300,
tz: obj.timezone,
},
};

Expand Down

0 comments on commit 399e182

Please sign in to comment.