Skip to content
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: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ yarn add localga --dev

```javascript
"scripts": {
"ga": "localga --id UA-XXXXXXXX-Y --folder ./your/js/folder"
"ga": "localga --id UA-XXXXXXXX-Y --folder ./your/js/folder --name google-analytics-local.js"
}
```

2. From command line (Install the module globally first):

```sh
localga --id UA-XXXXXXXX-Y --folder ./your/js/folder
localga --id UA-XXXXXXXX-Y --folder ./your/js/folder --name google-analytics-local.js
```

The `localga` module will generate two new script files called `google-analytics-local.js` and `analytics.js` placed in the folder you provided.
Expand Down
9 changes: 7 additions & 2 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ const cli = meow(
Options
--id Your Google Analytics ID
--folder Where to write the file
--name Name of the gtag.js script file

Usage Examples
$ localga --id UA-XXXXXXX-Y --folder ./src/js/
$ localga --id UA-XXXXXXX-Y --folder ./src/js/ --name google-analytics-local.js
`,
{
flags: {
Expand All @@ -30,7 +31,11 @@ Usage Examples
folder: {
type: 'string',
default: './'
}
},
name: {
type: 'string',
default: 'google-analytics-local.js'
},
}
}
);
Expand Down
15 changes: 8 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ const request = require('request-promise-native');
/**
* Internal dependencies
*/
const { writeFileSync, unlinkSync, existsSync } = require('fs');
const { writeFileSync, unlinkSync, existsSync, mkdirSync } = require('fs');
const path = require('path');

/**
* Google analytics root url
*/
const FILE_NAME = 'google-analytics-local.js';
const GA_SCRIPT_URL = 'https://www.googletagmanager.com/gtag/js';

const ANALYTICS_FILE_NAME = 'analytics.js';
Expand All @@ -22,11 +22,13 @@ const saveFile = (file, data) => {
unlinkSync(file);
}

mkdirSync(path.dirname(file), {recursive: true});

writeFileSync(file, data);
};

const saveAnalyticsFile = folder => {
const file = `${folder}/${ANALYTICS_FILE_NAME}`;
const file = path.join(folder, ANALYTICS_FILE_NAME);

return request(ANALYTICS_SCRIPT_URL)
.then(data => saveFile(file, data))
Expand All @@ -41,16 +43,16 @@ const saveAnalyticsFile = folder => {
* @return {Void}
*/
const localga = options => {
const { id, folder } = options;
const file = `${folder}/${FILE_NAME}`;
const { id, folder, name } = options;
const file = path.join(folder, name);

if (!id) {
throw new Error('No google analytics ID supplied.');
}

return request(`${GA_SCRIPT_URL}?id=${id}`)
.then(async data => {
data = data.replace(ANALYTICS_SCRIPT_URL, `${folder}/${ANALYTICS_FILE_NAME}`);
data = data.replace(ANALYTICS_SCRIPT_URL, path.join(folder, ANALYTICS_FILE_NAME));

saveFile(file, data);

Expand All @@ -62,5 +64,4 @@ const localga = options => {
module.exports = localga;
module.exports.localga = localga;

module.exports.FILE_NAME = FILE_NAME;
module.exports.ANALYTICS_FILE_NAME = ANALYTICS_FILE_NAME;
10 changes: 6 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ const { existsSync, mkdirSync } = require('fs');
* Test environment
*/
const DIR = './tmp';
const NAME = 'ga-local.js';
const tape = require('tape');
const { localga, FILE_NAME, ANALYTICS_FILE_NAME } = require('./');
const { localga, ANALYTICS_FILE_NAME } = require('./');

/**
* Init
Expand All @@ -20,16 +21,17 @@ if (!existsSync(DIR)) {

localga({
id: 'UA-83446952-1',
folder: DIR
folder: DIR,
name: NAME
}).then(() => {
/**
* Test if a master file is created
*/
tape('Should have a master file', t => {
const file = resolve(__dirname, `${DIR}/${FILE_NAME}`);
const file = resolve(__dirname, `${DIR}/${NAME}`);
const fileExists = existsSync(file);

t.ok(fileExists, `${FILE_NAME} exists`);
t.ok(fileExists, `${NAME} exists`);
t.end();
});

Expand Down
Loading