Skip to content

Commit

Permalink
Merge pull request #117 from SizeMeCom/I115
Browse files Browse the repository at this point in the history
Check existence of sizeme_options in all places where it is accessed
  • Loading branch information
tjjalava authored Feb 14, 2019
2 parents 4613107 + d96fd4b commit 6a41fce
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 164 deletions.
9 changes: 9 additions & 0 deletions nosizeme.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!doctype html>
<html>
<head>
<title>No Sizeme</title>
</head>
<body>
<h1>Nothing to see here, move along</h1>
</body>
</html>
120 changes: 65 additions & 55 deletions src/api/ga.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global ga, sizeme_options */
/* global ga */
import cookie from "react-cookie";

const PENDING = -1;
Expand All @@ -17,65 +17,75 @@ const optanonConsent = () => {
}
};

((i, s, o, g, r) => {
if (!i[r]) {
i["GoogleAnalyticsObject"] = r;
i[r] = i[r] ||
function () {
(i[r].q = i[r].q || []).push(arguments);
};
i[r].l = 1 * new Date();
let a = s.createElement(o);
let m = s.getElementsByTagName(o)[0];
a.async = 1;
a.src = g;
m.parentNode.insertBefore(a, m);
}
})(window, document, "script", "https://www.google-analytics.com/analytics.js", "ga");

const gaTrackingId = !sizeme_options.debugState ? prodTrackingId : devTrackingId;
let trackEvent;
let gaConsent = () => ENABLED;
const trackingConsentMethod = sizeme_options.trackingConsentMethod;
const gaQueue = [() => {
ga("create", gaTrackingId, "auto", { name: "sizemeTracker" });
}];

if (trackingConsentMethod) {
switch (trackingConsentMethod.toLowerCase()) {
case "optanon":
gaConsent = optanonConsent;
break;

default:
let trackEvent = () => {};

(sizemeOptions => {
if (!sizemeOptions) {
return;
}
}

const _trackEvent = (action, label) => {
ga("sizemeTracker.send", {
hitType: "event",
eventCategory: window.location.hostname,
eventAction: action,
eventLabel: label + " (v3)"
});
};

((i, s, o, g, r) => {
if (!i[r]) {
i["GoogleAnalyticsObject"] = r;
i[r] = i[r] ||
function () {
(i[r].q = i[r].q || []).push(arguments);
};
i[r].l = 1 * new Date();
let a = s.createElement(o);
let m = s.getElementsByTagName(o)[0];
a.async = 1;
a.src = g;
m.parentNode.insertBefore(a, m);
}
})(window, document, "script", "https://www.google-analytics.com/analytics.js", "ga");

trackEvent = (action, label) => {
switch (gaConsent()) {
case PENDING:
gaQueue.push(() => _trackEvent(action, label));
break;
const gaTrackingId = !sizemeOptions.debugState ? prodTrackingId : devTrackingId;
let gaConsent = () => ENABLED;
const trackingConsentMethod = sizemeOptions.trackingConsentMethod;
const gaQueue = [() => {
ga("create", gaTrackingId, "auto", {name: "sizemeTracker"});
}];

case ENABLED:
gaQueue.forEach(fn => fn());
_trackEvent(action, label);
trackEvent = _trackEvent;
break;
if (trackingConsentMethod) {
switch (trackingConsentMethod.toLowerCase()) {
case "optanon":
gaConsent = optanonConsent;
break;

case DISABLED:
trackEvent = () => {};
default:
}
}
};

const _trackEvent = (action, label) => {
ga("sizemeTracker.send", {
hitType: "event",
eventCategory: window.location.hostname,
eventAction: action,
eventLabel: label + " (v3)"
});
};


trackEvent = (action, label) => {
switch (gaConsent()) {
case PENDING:
gaQueue.push(() => _trackEvent(action, label));
break;

case ENABLED:
gaQueue.forEach(fn => fn());
_trackEvent(action, label);
trackEvent = _trackEvent;
break;

case DISABLED:
trackEvent = () => {
};
}
};
// eslint-disable-next-line
})(window.sizeme_options);


export { trackEvent };
90 changes: 50 additions & 40 deletions src/api/sizeme-api.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global sizeme_options, sizeme_product */
/* global sizeme_product */

import "isomorphic-fetch";
import { trackEvent } from "./ga.js";
Expand All @@ -14,52 +14,62 @@ import uiOptions from "./uiOptions";
import equals from "shallow-equals";
import cookie from "react-cookie";

const contextAddress = sizeme_options.contextAddress || "https://www.sizeme.com";
const pluginVersion = sizeme_options.pluginVersion || "UNKNOWN";
const sizemeOptions = () => window.sizeme_options || {};

const contextAddress = sizemeOptions().contextAddress || "https://www.sizeme.com";
const pluginVersion = sizemeOptions().pluginVersion || "UNKNOWN";
const cdnLocation = "https://cdn.sizeme.com";
const storeMeasurementsKey = "sizemeMeasurements";

const sizemeStore = createStore(rootReducer, applyMiddleware(
thunkMiddleware,
createLogger({
predicate: () => sizeme_options.debugState,
duration: true
})
));

function observeStore (select, onChange) {
let currentState;

function handleChange () {
let nextState = select(sizemeStore.getState());
if (!equals(nextState, currentState)) {
currentState = nextState;
onChange(currentState);
}
const sizemeStore = (sizemeOpts => {
if (!sizemeOpts) {
return {};
}

let unsubscribe = sizemeStore.subscribe(handleChange);
handleChange();
return unsubscribe;
}

observeStore(
({ productInfo, selectedProfile, abStatus }) => ({ product: productInfo.product, selectedProfile, abStatus }),
({ product, selectedProfile, abStatus }) => {
let smAction;
const statusPostFix = abStatus ? "-" + abStatus : "";
if (!product) {
smAction = "noProduct" + statusPostFix;
} else if (!Object.values(selectedProfile.measurements).some(item => item)) {
smAction = "noHuman";
} else if (!selectedProfile.id) {
smAction = "hasUnsaved";
} else {
smAction = "hasProfile";
const store = createStore(rootReducer, applyMiddleware(
thunkMiddleware,
createLogger({
predicate: () => sizemeOpts.debugState,
duration: true
})
));

function observeStore (select, onChange) {
let currentState;

function handleChange () {
let nextState = select(store.getState());
if (!equals(nextState, currentState)) {
currentState = nextState;
onChange(currentState);
}
}
cookie.save("sm_action", smAction, { path: "/" });

let unsubscribe = store.subscribe(handleChange);
handleChange();
return unsubscribe;
}
);

observeStore(
({productInfo, selectedProfile, abStatus}) => ({product: productInfo.product, selectedProfile, abStatus}),
({product, selectedProfile, abStatus}) => {
let smAction;
const statusPostFix = abStatus ? "-" + abStatus : "";
if (!product) {
smAction = "noProduct" + statusPostFix;
} else if (!Object.values(selectedProfile.measurements).some(item => item)) {
smAction = "noHuman";
} else if (!selectedProfile.id) {
smAction = "hasUnsaved";
} else {
smAction = "hasProfile";
}
cookie.save("sm_action", smAction, {path: "/"});
}
);

return store;
})(window.sizeme_options);

class FitRequest {
constructor (subject, item) {
Expand Down
12 changes: 8 additions & 4 deletions src/api/uiOptions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* global sizeme_options */

const general = {
disableSizeGuide: false,
toggler: false
Expand All @@ -26,5 +24,11 @@ const shops = {
}
};

export default Object.assign({ shopType: sizeme_options.shopType }, general,
shops[sizeme_options.shopType], sizeme_options.uiOptions);
export default (sizemeOptions => {
if (sizemeOptions) {
return Object.assign({ shopType: sizemeOptions.shopType }, general,
shops[sizemeOptions.shopType], sizemeOptions.uiOptions);
} else {
return {};
}
})(window.sizeme_options);
6 changes: 4 additions & 2 deletions src/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ i18n.init({
}
});

if (sizeme_options.additionalTranslations) {
const addtr = sizeme_options.additionalTranslations;
const additionalTranslations = window.sizeme_options ? sizeme_options.additionalTranslations : null;

if (additionalTranslations) {
const addtr = additionalTranslations;
["en", "fi", "sv", "ar"].forEach(lng => {
if (addtr[lng]) {
i18n.addResourceBundle(lng, "translation", addtr[lng], true, true);
Expand Down
Loading

0 comments on commit 6a41fce

Please sign in to comment.