Skip to content

Commit

Permalink
feat: analytics module
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Jul 12, 2020
1 parent e759e78 commit a1af2f5
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 1 deletion.
5 changes: 5 additions & 0 deletions core/core/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,11 @@ export interface RunOnlyConfiguration {
*
*/
components?: Record<string, any>;

/**
* analytics options
*/
analytics?: any;
}

export type RunConfiguration = RunOnlyConfiguration &
Expand Down
2 changes: 2 additions & 0 deletions core/store/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@
},
"license": "MIT",
"dependencies": {
"@analytics/google-analytics": "^0.4.0",
"@component-controls/core": "^1.8.0",
"@component-controls/loader": "^1.8.0",
"analytics": "^0.5.2",
"broadcast-channel": "^3.1.0",
"typescript": "^3.8.3"
},
Expand Down
29 changes: 29 additions & 0 deletions core/store/src/Store/Store.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Analytics from 'analytics';
import googleAnalytics from '@analytics/google-analytics';
import {
StoriesStore,
Pages,
Expand Down Expand Up @@ -40,6 +42,7 @@ export class Store implements StoryStore {
private observers: StoreObserver[];
private moduleId: number;
private _cachedPages: { [key: string]: Pages } = {};
private _analytics: any = null;
private _categoryItems: {
[key: string]: {
[key: string]: number;
Expand Down Expand Up @@ -73,6 +76,7 @@ export class Store implements StoryStore {
}
};
}
this.initializeAnalytics();
}

/**
Expand Down Expand Up @@ -372,4 +376,29 @@ export class Store implements StoryStore {
}
return this.loadedStore;
};
initializeAnalytics = () => {
if (this.loadedStore) {
const options = this.loadedStore.config?.analytics;
if (options) {
if (typeof options === 'string') {
this._analytics = Analytics({
app: this.loadedStore.config?.siteTitle,
plugins: [
googleAnalytics({
trackingId: options,
}),
],
});
} else {
this._analytics = Analytics(options);
}
}
}
};
visitPage = () => {
debugger;
if (this._analytics) {
this._analytics.page();
}
};
}
1 change: 1 addition & 0 deletions core/store/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export interface StoryStore {
) => StoriesStore | undefined;
addObserver: (observer: StoreObserver) => void;
removeObserver: (observer: StoreObserver) => void;
visitPage: () => void;
}

export const UPDATE_STORY_MSG = 'component_controls_update_story';
Expand Down
1 change: 1 addition & 0 deletions core/store/src/typings.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
declare module '@component-controls/loader/story-store-data';
declare module '@analytics/google-analytics';
1 change: 1 addition & 0 deletions examples/gatsby/.config/runtime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { OctofaceIcon } from '@primer/octicons-react';
const categories = ['Introduction', 'Application','Controls','Blocks', 'Editors', 'Components', 'Plugins']

const config: RunOnlyConfiguration = {
analytics: 'UA-172446254-1',
siteTitle: `Component controls`,
siteTitleAlt: `Component controls - https://github.com/ccontrols/component-controls`,
siteHeadline: `Component controls gatsby`,
Expand Down
1 change: 1 addition & 0 deletions ui/blocks/src/context/block/BlockContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export const BlockContextProvider: React.FC<BlockContextInputProps> = ({
storyId =
doc && doc.stories && doc.stories.length ? doc.stories[0] : undefined;
}
store.visitPage();
return (
<RecoilRoot>
<ErrorBoundary>
Expand Down
50 changes: 49 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@
# yarn lockfile v1


"@analytics/cookie-utils@^0.2.3":
version "0.2.3"
resolved "https://registry.yarnpkg.com/@analytics/cookie-utils/-/cookie-utils-0.2.3.tgz#e6ab923f88d89f7b02da0cfab585ff193977052f"
integrity sha512-RiMAVpSluRbWb2hlT9wMJ0r2l+MUZzScYjY+w2iWRzjOr9Zzzs4tYzJT6Sd94PDz3LzCuf4aGOwS6pkKXTEBLw==

"@analytics/google-analytics@^0.4.0":
version "0.4.0"
resolved "https://registry.yarnpkg.com/@analytics/google-analytics/-/google-analytics-0.4.0.tgz#5ca47a5f5f4b1446212f4fe293edbb061f4119b5"
integrity sha512-IuXKIdThT/AqUxeOY3Oe8TCFd6JsAzdXUHtnzCGwQyCH1SzEQwjvSqBnBrGdZdg+FbstNAgpeBIRdffa6vQRUQ==
dependencies:
universal-analytics "^0.4.20"

"@analytics/storage-utils@^0.2.3":
version "0.2.3"
resolved "https://registry.yarnpkg.com/@analytics/storage-utils/-/storage-utils-0.2.3.tgz#944a575cb54b0291d2a1abbdefde82254c74f929"
integrity sha512-hk2ZU5aAuap2L94UdsT5z5xHcmDPGker4DK+OxYnJVQyj2NdqkVMVRFUOUqHxdMP3c9d6oLunQJeYblH5NDNWQ==
dependencies:
"@analytics/cookie-utils" "^0.2.3"

"@ardatan/[email protected]":
version "0.0.1"
resolved "https://registry.yarnpkg.com/@ardatan/aggregate-error/-/aggregate-error-0.0.1.tgz#1403ac5de10d8ca689fc1f65844c27179ae1d44f"
Expand Down Expand Up @@ -5228,6 +5247,21 @@ alphanum-sort@^1.0.0:
resolved "https://registry.yarnpkg.com/alphanum-sort/-/alphanum-sort-1.0.2.tgz#97a1119649b211ad33691d9f9f486a8ec9fbe0a3"
integrity sha1-l6ERlkmyEa0zaR2fn0hqjsn74KM=

analytics-utils@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/analytics-utils/-/analytics-utils-0.2.1.tgz#ecd255e7d23102a9fcd5705b1287fbb23bc88e67"
integrity sha512-cwaH/VsKARvixb1kcaigZLpcIe4O0L64scbHixDIsnf6gJoFOK8803erS2e0RzI/pXE92U5BOVDVNJuskqEWsQ==
dependencies:
"@analytics/storage-utils" "^0.2.3"
dlv "^1.1.3"

analytics@^0.5.2:
version "0.5.2"
resolved "https://registry.yarnpkg.com/analytics/-/analytics-0.5.2.tgz#e9b93bd78dd07d67f87a9cef28355e00ad74d8ae"
integrity sha512-9oa0n90n4DOMIHgPYWA1oudiFcsDDVDU5UbZHKM42KM4I3hZhsijxUI93OIAT2O/TNg0ppMRQSREyUcJ6cdOhw==
dependencies:
analytics-utils "^0.2.1"

ansi-align@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/ansi-align/-/ansi-align-3.0.0.tgz#b536b371cf687caaef236c18d3e21fe3797467cb"
Expand Down Expand Up @@ -8622,6 +8656,11 @@ dir-glob@^3.0.1:
dependencies:
path-type "^4.0.0"

dlv@^1.1.3:
version "1.1.3"
resolved "https://registry.yarnpkg.com/dlv/-/dlv-1.1.3.tgz#5c198a8a11453596e751494d49874bc7732f2e79"
integrity sha512-+HlytyjlPKnIG8XuRG8WvmBP8xs8P71y+SKKS6ZXWoEgLuePxtDoUEiH7WkdePWrQ5JBpE6aoVqfZfJUQkjXwA==

dns-equal@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/dns-equal/-/dns-equal-1.0.0.tgz#b39e7f1da6eb0a75ba9c17324b34753c47e0654d"
Expand Down Expand Up @@ -18203,7 +18242,7 @@ request-promise-native@^1.0.5:
stealthy-require "^1.1.1"
tough-cookie "^2.3.3"

request@^2.72.0, request@^2.87.0:
request@^2.72.0, request@^2.87.0, request@^2.88.2:
version "2.88.2"
resolved "https://registry.yarnpkg.com/request/-/request-2.88.2.tgz#d73c918731cb5a87da047e207234146f664d12b3"
integrity sha512-MsvtOrfG9ZcrOwAW+Qi+F6HbD0CWXEh9ou77uOb7FM2WPhwT7smM833PzanhJLsgXjN89Ir6V2PczXNnMpwKhw==
Expand Down Expand Up @@ -20674,6 +20713,15 @@ unist-util-visit@^2.0.0, unist-util-visit@^2.0.2:
unist-util-is "^4.0.0"
unist-util-visit-parents "^3.0.0"

universal-analytics@^0.4.20:
version "0.4.23"
resolved "https://registry.yarnpkg.com/universal-analytics/-/universal-analytics-0.4.23.tgz#d915e676850c25c4156762471bdd7cf2eaaca8ac"
integrity sha512-lgMIH7XBI6OgYn1woDEmxhGdj8yDefMKg7GkWdeATAlQZFrMrNyxSkpDzY57iY0/6fdlzTbBV03OawvvzG+q7A==
dependencies:
debug "^4.1.1"
request "^2.88.2"
uuid "^3.0.0"

universal-user-agent@^4.0.0:
version "4.0.1"
resolved "https://registry.yarnpkg.com/universal-user-agent/-/universal-user-agent-4.0.1.tgz#fd8d6cb773a679a709e967ef8288a31fcc03e557"
Expand Down

0 comments on commit a1af2f5

Please sign in to comment.