Skip to content

Commit 51c7837

Browse files
authored
feat: add channel parameter (#72)
1 parent 2114e36 commit 51c7837

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/index.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ test.each([
4444
{ url: "https://example.com/js" },
4545
"https://example.com/js?callback=__googleMapsCallback",
4646
],
47+
[
48+
{ channel: "foo" },
49+
"https://maps.googleapis.com/maps/api/js?callback=__googleMapsCallback&channel=foo",
50+
],
4751
])("createUrl is correct", (options: LoaderOptions, expected: string) => {
4852
const loader = new Loader(options);
4953
expect(loader.createUrl()).toEqual(expected);

src/index.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,33 @@ export interface LoaderOptions {
4141
* See https://developers.google.com/maps/documentation/javascript/get-api-key.
4242
*/
4343
apiKey: string;
44+
/**
45+
* To track usage across different applications using the same client ID,
46+
* you may provide an optional channel parameter with your requests. By
47+
* specifying different channel values for different aspects of your
48+
* application, you can determine precisely how your application is used.
49+
*
50+
* For example, your externally-facing website may access the API using a
51+
* channel set to customer while your internal marketing department may use
52+
* a channel set to mkting. Your reports will break down usage by those
53+
* channel values.
54+
*
55+
* Channel reporting is available for applications using the Maps JavaScript
56+
* API, the image APIs or any of the Google Maps Platform web services.
57+
*
58+
* The channel parameter must use the following format:
59+
*
60+
* - Must be an ASCII alphanumeric string.
61+
* - Period (.), underscore (_) and hyphen (-) characters are allowed.
62+
* - The channel parameter is case-insensitive; upper-case, mixed-case, and
63+
* lower-cased channel parameters will be merged into their lower-case
64+
* equivalent. For example, usage on the `CUSTOMER` channel will be combined
65+
* with the usage on the `customer` channel.
66+
* - The channel value must be a static value assigned per application
67+
* instance, and must not be generated dynamically. You may not use
68+
* channel values to track individual users, for example.
69+
*/
70+
channel?: string;
4471
/**
4572
* In your application you can specify release channels or version numbers:
4673
*
@@ -68,7 +95,7 @@ export interface LoaderOptions {
6895
*
6996
* If you do not explicitly specify a version, you will receive the
7097
* weekly version by default.
71-
*/
98+
*/
7299
version?: string;
73100
/**
74101
* The id of the script tag. Before adding a new script, the Loader will check for an existing one.
@@ -180,6 +207,10 @@ export class Loader {
180207
/**
181208
* See [[LoaderOptions.id]]
182209
*/
210+
channel: string;
211+
/**
212+
* See [[LoaderOptions.channel]]
213+
*/
183214
id: string;
184215
/**
185216
* See [[LoaderOptions.libraries]]
@@ -227,6 +258,7 @@ export class Loader {
227258
*/
228259
constructor({
229260
apiKey,
261+
channel,
230262
id = "__googleMapsScriptId",
231263
libraries = [],
232264
language,
@@ -238,6 +270,7 @@ export class Loader {
238270
}: LoaderOptions) {
239271
this.version = version;
240272
this.apiKey = apiKey;
273+
this.channel = channel;
241274
this.id = id;
242275
this.libraries = libraries;
243276
this.language = language;
@@ -260,6 +293,10 @@ export class Loader {
260293
url += `&key=${this.apiKey}`;
261294
}
262295

296+
if (this.channel) {
297+
url += `&channel=${this.channel}`;
298+
}
299+
263300
if (this.libraries.length > 0) {
264301
url += `&libraries=${this.libraries.join(",")}`;
265302
}

0 commit comments

Comments
 (0)