Skip to content

feat: add channel parameter #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 12, 2020
Merged
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
4 changes: 4 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ test.each([
{ url: "https://example.com/js" },
"https://example.com/js?callback=__googleMapsCallback",
],
[
{ channel: "foo" },
"https://maps.googleapis.com/maps/api/js?callback=__googleMapsCallback&channel=foo",
],
])("createUrl is correct", (options: LoaderOptions, expected: string) => {
const loader = new Loader(options);
expect(loader.createUrl()).toEqual(expected);
Expand Down
39 changes: 38 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,33 @@ export interface LoaderOptions {
* See https://developers.google.com/maps/documentation/javascript/get-api-key.
*/
apiKey: string;
/**
* To track usage across different applications using the same client ID,
* you may provide an optional channel parameter with your requests. By
* specifying different channel values for different aspects of your
* application, you can determine precisely how your application is used.
*
* For example, your externally-facing website may access the API using a
* channel set to customer while your internal marketing department may use
* a channel set to mkting. Your reports will break down usage by those
* channel values.
*
* Channel reporting is available for applications using the Maps JavaScript
* API, the image APIs or any of the Google Maps Platform web services.
*
* The channel parameter must use the following format:
*
* - Must be an ASCII alphanumeric string.
* - Period (.), underscore (_) and hyphen (-) characters are allowed.
* - The channel parameter is case-insensitive; upper-case, mixed-case, and
* lower-cased channel parameters will be merged into their lower-case
* equivalent. For example, usage on the `CUSTOMER` channel will be combined
* with the usage on the `customer` channel.
* - The channel value must be a static value assigned per application
* instance, and must not be generated dynamically. You may not use
* channel values to track individual users, for example.
*/
channel?: string;
/**
* In your application you can specify release channels or version numbers:
*
Expand Down Expand Up @@ -68,7 +95,7 @@ export interface LoaderOptions {
*
* If you do not explicitly specify a version, you will receive the
* weekly version by default.
*/
*/
version?: string;
/**
* The id of the script tag. Before adding a new script, the Loader will check for an existing one.
Expand Down Expand Up @@ -180,6 +207,10 @@ export class Loader {
/**
* See [[LoaderOptions.id]]
*/
channel: string;
/**
* See [[LoaderOptions.channel]]
*/
id: string;
/**
* See [[LoaderOptions.libraries]]
Expand Down Expand Up @@ -227,6 +258,7 @@ export class Loader {
*/
constructor({
apiKey,
channel,
id = "__googleMapsScriptId",
libraries = [],
language,
Expand All @@ -238,6 +270,7 @@ export class Loader {
}: LoaderOptions) {
this.version = version;
this.apiKey = apiKey;
this.channel = channel;
this.id = id;
this.libraries = libraries;
this.language = language;
Expand All @@ -260,6 +293,10 @@ export class Loader {
url += `&key=${this.apiKey}`;
}

if (this.channel) {
url += `&channel=${this.channel}`;
}

if (this.libraries.length > 0) {
url += `&libraries=${this.libraries.join(",")}`;
}
Expand Down