Last active
December 15, 2015 00:02
-
-
Save bourroush/6f68004ae35cf9e22b1c to your computer and use it in GitHub Desktop.
Segment.io Analytics functions
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
/ | |
/ Segment.io | |
/ | |
javascript: | |
!function(){var analytics=window.analytics=window.analytics||[];if(!analytics.initialize)if(analytics.invoked)window.console&&console.error&&console.error("Segment snippet included twice.");else{analytics.invoked=!0;analytics.methods=["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","reset","group","track","ready","alias","page","once","off","on"];analytics.factory=function(t){return function(){var e=Array.prototype.slice.call(arguments);e.unshift(t);analytics.push(e);return analytics}};for(var t=0;t<analytics.methods.length;t++){var e=analytics.methods[t];analytics[e]=analytics.factory(e)}analytics.load=function(t){var e=document.createElement("script");e.type="text/javascript";e.async=!0;e.src=("https:"===document.location.protocol?"https://":"http://")+"cdn.segment.com/analytics.js/v1/"+t+"/analytics.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(e,n)};analytics.SNIPPET_VERSION="3.1.0"; | |
analytics.load("#{ENV['SEGMENT_KEY']}"); | |
}}(); |
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
ul.l--naked.l--hori--pushRight.g-clear | |
li.l-el.p-head-sub-wrp.p-head-item | |
a.p-head-item-a.c-bold.p-head-sub-trg.tablet-hide ng-href="/#!/products" ga-event="['menu-level-one', 'click', 'shop']" ng-click="toggleDesktopMenu()" SHOP | |
a.p-head-item-a.c-bold.p-head-sub-trg.tablet-show ng-click="toggleDesktopMenu()" SHOP | |
ul.p-head-sub-ul | |
li.p-head-sub-col | |
ul.l--naked | |
li.l-el ng-repeat="product in catalog.navigationProducts()" ng-if="$index < 5" | |
a ng-href="{{product.permalink}}" ng-bind="product.name" ga-event="['menu-level-two', 'click', '{{product.slug}}']" | |
li.p-head-sub-col | |
ul.l--naked | |
li.l-el ng-repeat="product in catalog.navigationProducts()" ng-if="$index > 4" | |
a ng-href="{{product.permalink}}" ng-bind="product.name" ga-event="['menu-level-two', 'click', '{{product.slug}}']" | |
li.p-head-sub-col.tablet-show | |
ul.l--naked | |
li.l-el | |
a ng-href="/#!/products" ga-event="['menu-level-one', 'click', 'shop']" All Products |
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
Glossier.directive 'gaEvent', ($parse, $location, Tracking, $log, segmentio) -> | |
restrict: 'A', | |
link: (scope, element, attrs) -> | |
options = $parse(attrs.gaEvent)({}) | |
element.on 'click', () -> | |
if (options.length > 1) | |
category = options[0] | |
action = options[1] | |
label = options[2] | |
value = options[3] || Tracking.variantEventValue() | |
analytics.track action, | |
category: category | |
label: label | |
value: value |
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
Glossier.controller "GlobalCtrl", ($rootScope, $scope, $state, Status, Account, $log, $window, $location, $anchorScroll, Modals, GeoIp, $timeout, ipCookie, Env, segmentio, $interval) -> | |
$scope.absOldUrl = '' | |
# | |
# Segment.io Analytics functions | |
# | |
$scope.analytics = | |
# Event tracking | |
trackEvent: (name) -> | |
analytics.track name | |
# Identify user | |
identifyTrack: (id, email, name = false) -> | |
if name | |
analytics.identify id, | |
email: email, | |
name: name | |
else | |
analytics.identify id, | |
email: email | |
# Pageview tracking | |
pageTracking: (fromState) -> | |
path = $location.path() | |
querystring = '' | |
referrer = '' | |
if path.indexOf('?') != -1 | |
querystring = path.substring(path.indexOf('?'), path.length) | |
if fromState.name | |
referrer = fromState.absOldUrl | |
analytics.page | |
path: path | |
referrer: referrer | |
search: querystring | |
url: $location.absUrl() | |
# Add product to cart | |
addProduct: (id, sku, name, price, quantity) -> | |
analytics.track 'Added Product', | |
id: id | |
sku: sku | |
name: name | |
price: parseFloat(price) | |
quantity: quantity | |
# | |
# get correct absOldUrl | |
# | |
$rootScope.$on '$locationChangeStart', (evt, absNewUrl, absOldUrl) -> | |
if absNewUrl != absOldUrl | |
$scope.absOldUrl = absOldUrl | |
$rootScope.$on '$stateChangeSuccess', (fromState) -> | |
# ANALYTICS | |
$timeout () -> | |
fromState.absOldUrl = $scope.absOldUrl | |
$scope.analytics.pageTracking(fromState) | |
, 2000 | |
$scope.init() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment