Skip to content

Commit

Permalink
GA plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
gmrchk committed Aug 2, 2018
1 parent a4eac96 commit 6f99d7e
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 0 deletions.
112 changes: 112 additions & 0 deletions dist/plugins/swupGaPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else if(typeof exports === 'object')
exports["swupGaPlugin"] = factory();
else
root["swupGaPlugin"] = factory();
})(window, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // define __esModule on exports
/******/ __webpack_require__.r = function(exports) {
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {

"use strict";


module.exports = {
name: 'swupGaPlugin',
options: { runScripts: false },
exec: function exec(options, swup) {
document.addEventListener('swup:contentReplaced', function () {
if (typeof window.ga === 'function') {
var title = document.title;
var url = window.location.pathname + window.location.search;

window.ga('set', 'title', title);
window.ga('set', 'page', url);
window.ga('send', 'pageview');

swup.log('GA pageview (url \'' + url + '\').');
} else {
console.warn('GA is not loaded.');
}
});
}
};

/***/ })
/******/ ]);
});
1 change: 1 addition & 0 deletions dist/plugins/swupGaPlugin.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
[Plugins](#plugins)
* [Plugin Installation](#plugin-installation)
* [swupMergeHeadPlugin](#swupmergeheadplugin)
* [swupGaPlugin](#swupgaplugin)
* [swupGtmPlugin](#swupgtmplugin)

[API](#api)

Expand Down Expand Up @@ -396,6 +398,18 @@ Merge Head Plugin replaces the html tags in head on each content replace (`swup:
Plugin has one option `runScripts`. If the options is set to `true`, script tags placed into head are executed (code inside of the tag as well as linked by `src` attribute).
Option defaults to `false`.

### swupGaPlugin
Google Analytics Plugin triggers `pageview` event on `swup:contentReplaced` (on each page change).
Note that this event is not triggered at the first load, so the first page view must be triggered elsewhere.
However, page view event is by default triggered in [Javascripts tracking snippet](https://developers.google.com/analytics/devguides/collection/analyticsjs/#the_javascript_tracking_snippet).
Simplified code run by this plugin on `swup:contentReplaced`:

```javascript
window.ga('set', 'title', document.title);
window.ga('set', 'page', window.location.pathname + window.location.search);
window.ga('send', 'pageview');
```

## API
The instance of the swup can be imported and used across your sites JavaScript to enable some additional features. When debug mode (see [options](#options) section) is enabled, instance is also available in `window` object as `window.swup`.
We can access some of the information used by swup such as used options:
Expand Down
20 changes: 20 additions & 0 deletions src/plugins/swupGaPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module.exports = {
name: 'swupGaPlugin',
options: {runScripts: false},
exec: function(options, swup) {
document.addEventListener('swup:contentReplaced', function () {
if (typeof window.ga === 'function') {
let title = document.title;
let url = window.location.pathname + window.location.search;

window.ga('set', 'title', title);
window.ga('set', 'page', url);
window.ga('send', 'pageview');

swup.log(`GA pageview (url '${url}').`);
} else {
console.warn('GA is not loaded.');
}
})
}
}

0 comments on commit 6f99d7e

Please sign in to comment.