Last active
January 29, 2019 09:36
-
-
Save afcapel/c83ff3f219e07eb6d7b727f7e5a1b245 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Controller } from 'stimulus' | |
export default class extends Controller { | |
connect() { | |
if ( typeof window.ga != 'undefined' ) { | |
const currentExperiments = this.experiments() | |
Object.keys(currentExperiments).forEach((experimentId) => { | |
ga('set', 'expId', experimentId) | |
ga('set', 'expVar', currentExperiments[experimentId]) | |
}) | |
ga('set', 'location', location.href.split('#')[0]) | |
ga('send', 'pageview', { "title": document.title }) | |
if ( this.event() ) { | |
const event = this.event() | |
ga('send', 'event', event['category'], event['action'], event['label']) | |
} | |
} | |
} | |
experiments() { | |
return JSON.parse(this.element.getAttribute("data-analytics-experiments")) | |
} | |
event() { | |
return JSON.parse(this.element.getAttribute("data-analytics-event")) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def analytics_controller_data | |
data = {} | |
data["controller"] = "analytics" | |
data["analytics-experiments"] = current_experiments.to_json | |
if flash[:analytics_event].present? | |
data["analytics-event"] = flash[:analytics_event].to_json | |
end | |
data | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment